From: daniel.kruegler@googlemail.com   
      
   On 2012-08-13 04:48, Kaba wrote:   
   > Consider the following code. I'd expect it to call the   
   > copy-constructor. However, both the Visual Studio 2010 and the   
   > gcc-compiler at Ideone run the third constructor.   
      
   Yes this is required behaviour.   
      
   > What are the rules in this case?   
      
   There is no much mystery involved here, the rules are based on normal   
   overload selection rules, see below.   
      
   > #include    
   >   
   > template    
   > class A   
   > {   
   > public:   
   > A() {}   
   >   
   > A(const A& that)   
   > {   
   > std::cout << "Copy" << std::endl;   
   > }   
   >   
   > A(A&& that)   
   > {   
   > std::cout << "Move" << std::endl;   
   > }   
   >   
   > template    
   > A(That&& that)   
   > {   
   > std::cout << "Wait... what?" << std::endl;   
   > }   
   > };   
   >   
   > int main()   
   > {   
   > A a;   
   > A b(a);   
   >   
   > return 0;   
   > }   
      
   You are providing an lvalue of type (non-const) A to an   
   direct-initialization context. The compiler sees   
      
   A::A(const A&) // Copy constructor   
   A::A(A&&) // Move constructor   
      
   and additionally would deduce   
      
   A::A(A&) // No copy constructor, albeit it has the   
   // same effective signature than A::A(A&)   
      
   from the template constructor based on the non-const lvalue of A.   
   The last one is a better match than any of the first two, so the usual   
   function-over-template rule cannot be applied here.   
      
   Note that the last signature is no copy-constructor, because   
   copy/move-constructors won't be instantiated from templates (see 12.8   
   p2+3).   
      
   Note also that the restriction rule 12.8 p6 does not apply here,   
   because the instantiated signature is valid.   
      
   As a general rule, you should always constrain a perfect forwarding   
   constructor and assignment operator with a single argument unless you   
   know pretty well what you are doing. In this case you should at least   
   "filter away" all argument types that would correspond with the class   
   template type (ignoring cv-qualifiers).   
      
   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)   
|