Copy constructor is being called first instead of only move constructor on passing even R-value reference

An moving assignment (not an assignment initialization) requires a move operator=(strings&&). It is not available if you defined own copy constructor. Note, that in situation like this

struct S {
    S() {}
    S(const S &obj) {  std::cout << "Copy ctor" << std::endl;}
    S(S &&obj) {  std::cout << "Move ctor" << std::endl;}
    S& operator=(const S &obj) {  std::cout << "Copy =" << std::endl; return *this;}
    S& operator=(S &&obj) {  std::cout << "Move =" << std::endl; return *this;}

int main()
    S a = S();
    a = S();

with C++11 compiler the declaration line wouldn’t even produce output, because no copy or move did happen, it was elided.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top