Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

Qt and Trivial Relocation (Part 3)

Trivial relocability for vector erasure, and types with write-through reference semantics


int a; double b;

std::tie(a, b) = getTuple();  // std::tie produces std::tuple<int &, double &>
                              // assignment will write through, over a and b

struct IntRef {
  int &m_ref;

  IntRef &operator=(const IntRef &other)
  {
    // write-through semantics for assignment:
    m_ref = other.m_ref;
    return *this;
  }
};

int a = 1;
int b = 2;
IntRef ra{a};
IntRef rb{b};

ra = rb;
std::println("{}, {}", a, b); // prints 2, 2; we've "assigned through" ra.

int a = 1, b = 2, c = 3, d = 4, e = 5;
IntRef ra{a}, rb{b}, rc{c}, rd{d}, re{e};

std::vector<IntRef> v{ra, rb, rc, rd, re};

v.erase(v.begin() + 1);

std::println("{}, {}, {}, {}, {}", a, b, c, d, e);

IntRef satisfies our definition of trivial relocability.

About KDAB


GiuseppeD'Angelo

Giuseppe D’Angelo

Senior Software Engineer

Learn Modern C++

Learn more