1

Assume we have a type A with a member that understands move semantics and we wish to implement move semantics for A as well;

struct A {
  ::std::vector<int> ints;
  A(A&&);
};

When implementing A::A(A&&) do we have to explicitly ::std::move the ints member or will it be passed as ::std::vector<int>&& automatically? That is, will these do the same:

A(A&& a) : ints(a.ints) {}              // version 1
A(A&& a) : ints(::std::move(a.ints)) {} // version 2
bitmask
  • 32,434
  • 14
  • 99
  • 159
  • 1
    Yes, you have to use `std::move` (or use the `defualt` move constructor.) I'm looking for a suitable duplicate. – juanchopanza Jun 13 '14 at 13:38
  • @juanchopanza: I wasn't sure so I first looked for a questions that gave a definite answer to this but didn't find one. If you have more luck, feel free to close as dupe. – bitmask Jun 13 '14 at 13:40
  • Closely related: http://stackoverflow.com/questions/4782757/rule-of-three-becomes-rule-of-five-with-c11 – 101010 Jun 13 '14 at 13:41
  • Rvalue reference parameters and variables are (as id-expressions) lvalues. So are data members (class-member-access-expressions) of lvalues. Therefore, `a.ints` is an lvalue, and `ints(a.ints)` will invoke the *copy* constructor. – dyp Jun 13 '14 at 13:42
  • @bitmask Yes, unfortunately a lot of the dupes have poor titles. I am pretty sure I have answered similar questions at least twice. Anyway, I found a reasonable duplicate. – juanchopanza Jun 13 '14 at 13:44

0 Answers0