int main(void) { std::list<std::string> l(10, "C++ Truths"); std::cout << l.size(); // prints 10 std::vector<std::string> v(std::make_move_iterator(begin(l)), std::make_move_iterator(end(l))); std::cout << l.size(); // prints 10 std::cout << v.size(); // prints 10 // prints 10 empty strings separated by #. (i.e, ##########) std::copy(begin(l), end(l), std::ostream_iterator<std::string>(std::cout, "#"); // prints "C++ Truths" 10 times. std::copy(begin(v), end(v), std::ostream_iterator<std::string>(std::cout, " "); }

char alpha='A'; std::vector<std::string> alpha_vector(26); // fill the vector with A,B,C,....X,Y,Z. std::generate(begin(alpha_vector), end(alpha_vector), [alpha]() mutable { return alpha++; }); // move all the strings from the vector to the string // no invariants broken here. std::set<std::string> alpha_set(std::make_move_iterator(begin(alpha_vector)), std::make_move_iterator(end(alpha_vector))); alpha_vector.clear(); // move each string back to the vector for(const std::string & s : alpha_set) { alpha_vector.push_back(std::move(const_cast<std::string &>(s))); } // prints 26 #s. That means the set has 26 empty strings, which // by definitions breaks the invariant of uniqueness. std::copy(begin(alpha_set), end(alpha_set), std::ostream_iterator<std::string>(std::cout, "#"));

class Foo { std::string key; std::vector<int> data; public: Foo(std::string k, std::vector<int> d) : key(std::move(k)), data(std::move(d)) {} Foo(const Foo &) = default; Foo & operator = (const Foo &) = default; #ifdef OPTION1 Foo(Foo && f) : key(f.key), // copies key data(std::move(f.data)) // moves data {} Foo & operator = (Foo && f) { using std::swap; this->key = f.key; swap(this->data, f.data); } #endif #ifdef OPTION2 Foo(Foo &&) = default; Foo & operator = (Foo &&) = default; Foo move() { // copy key and move data. return { key, std::move(data) }; } #endif };

std::set<Foo> foo_set; foo_set.insert(...); std::vector<Foo> foo_vector; foo_vector.reserve(foo_set.size()); for(auto iter = begin(foo_set); iter != end(foo_set);) { #ifdef OPTION1 foo_vector.push_back(std::move(const_cast<Foo &>(*iter))); #else foo_vector.push_back(const_cast<Foo &>(*iter).move()); #endif foo_set.erase(iter++); }

std::vector<std::string> attempt_move(std::initializer_list<in<std::string>> list) { std::vector<std::string> result; result.reserve(list.size()); for(const in<std::string> & s : list) { if(s.rvalue()) { // rvalue string. Just move. // in<T>.rget() returns T&& result.push_back(s.rget()); } else { // lvalue string. Make a copy // in<T>.get() returns const T &. result.emplace_back(s.get()); } } return result; } int main(int argc, char *argv[]) { if(argc >= 4) { std::string s0(argv[0]); std::string s1(argv[1]); attempt_move({ s0, std::move(s1), argv[2], argv[3] }); } }