f2068815   
   From: daniel.kruegler@googlemail.com   
      
   Am 05.11.2011 22:19, schrieb Frank Bergemann:   
   > I could cut it back to the essentials now:   
   >   
   > #include   
   > #include   
   >   
   > /*   
   > * a test class to check,   
   > * if copying or assignment is used   
   > */   
   > class TestClass   
   > {   
   > public:   
   > TestClass()   
   > {};   
   >   
   > ~TestClass()   
   > {};   
   >   
   > TestClass(   
   > const TestClass& rhs)   
   > {   
   > std::cerr<< "!!! TestClass copy c'tor invoked !!!"<< std::endl;   
   > };   
   >   
   > TestClass& operator=(   
   > const TestClass& rhs)   
   > {   
   > std::cerr<< "!!! TestClass assignment operator invoked !!!"<<   
   > std::endl;   
   > return *this;   
   > }   
   > };   
   >   
   > int   
   > main(   
   > int argc,   
   > char ** argv)   
   > {   
   > std::tuple myTuple(std::forward_as_tuple(TestClass()));   
   > return 0;   
   > }   
   >   
   > Why does that one tell   
   >> ./TestTuple   
   > !!! TestClass copy c'tor invoked !!!   
   >   
   > Why is TestClass copied here?   
      
   The following is going on here: You are default-constructing a TestClass   
   which is provided as an rvalue to std::forward_as_tuple. The effect is,   
   that a temporary std::tuple is created, provided as another   
   rvalue to a constructor of std::tuple.   
   Of the constructor overloads of this class, the best match is   
      
   template    
   tuple(tuple&& u);   
      
   Within this constructor std::get<0>() is called with the lvalue u, but   
   the result is transformed via std::forward, which has the   
   same effect as if invoking std::move. The result of this call is another   
   rvalue (more precisely xvalue) of type TestClass. This rvalue is   
   presented to TestClass, which has a copy-constructor that will be   
   selected here. If the type had a move-constructor, it would have been   
   preferred.   
      
   HTH & Greetings from Bremen,   
      
   Daniel Krügler   
      
      
   --   
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
    [ comp.lang.c++.moderated. First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|