c++ - Passing a temporary as a reference -
i trying understand copy , swap idiom through post. answer posted has following code in it
class dumb_array { public: // ... friend void swap(dumb_array& first, dumb_array& second) // nothrow { // enable adl (not necessary in our case, practice) using std::swap; // swapping members of 2 classes, // 2 classes swapped swap(first.msize, second.msize); swap(first.marray, second.marray); } // move constructor dumb_array(dumb_array&& other) : dumb_array() // initialize via default constructor, c++11 { swap(*this, other); //<------question statement } // ... }; i noticed author used statement
swap(*this, other); other temporary or rvalue being passed reference method swap. not sure if pass rvalue reference. in order test tried doing following not work until convert parameter const reference
void myfunct(std::string& f) { std::cout << "hello"; } int main() { myfunct(std::string("dsdsd")); } my question how can other being temporary passed reference in swap(*this, other); while myfunct(std::string("dsdsd")); cant passed reference.
the constructor takes rvalue reference, other lvalue (it has name).
Comments
Post a Comment