Is duplicate movement on the same object copied from left to right?

I am just starting to work in C ++ 11 mode, so I play with it. But I found something that I can’t understand.

#include <iostream>
using namespace std;

class A{
    public:
        A(){cout << "default ctor" << endl;}
        A(const string& str):_str{str}{cout << "parameter ctor" << endl;}
        A(A&& obj):_str{std::move(obj._str)}{cout << "move ctor" << endl;}
        A& operator =(A&& rhs){_str = std::move(rhs._str);cout << "move assignment operation" << endl; return *this;}
        void print(){cout << _str << endl;}
    private:
        string _str;
};

int main(){
    A a("rupesh yadav"); // parameter ctor
    A b(std::move(a));   // move ctor

    cout << "print a: ";
    a.print();           // NOT printing  --> CORRECT!!
    cout << "print b: ";
    b.print();           // printing      --> CORRECT!!

    b = std::move(a);    // i don't know may be silly but still lets do it WHY NOT!!!, could be just mistake??

    cout << "print a: "; 
    a.print();           // printing      --> WRONG!! 
    cout << "print b: "; 
    b.print();           // NOT printing  --> WRONG!!
}

I expected that the operation b = std::move(a)will behave differently, because I apply the transition to object a a second time, but it copies the left object b to the right side object a , I don’t understand this part.

Or I did something wrong in programming. Please help if I do something wrong in the move operation.

EDIT: , undefined. , , a b, , b a?

, , ?

+4
2

.

, a b, a ", " ( ). a b! . ( , .)

. , .

+5

. ", " . , " , " 17.3.28. , std::string :

basic_string(const basic_string& str);

basic_string(basic_string&& str) noexcept;

2 : basic_string, [tab: strings.ctr.cpy]. str .

, . , , undefined . , , std::string undefined, :

operator<< std::string ? cppreference :

FormattedOutputFunction. :

  • str.size() os.width(), [str.begin(), str.end()) as-is

  • , (os.flags() & ios_base::adjustfield) == ios_base::left, os.width()-str.size() os.fill()

  • os.width()-str.size() os.fill()

size(), begin() end() , .

+2

All Articles