home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,086 of 33,346   
   Kaba to All   
   Implicit move of an lvalue   
   03 Apr 12 11:36:06   
   
   From: kaba@nowhere.com   
      
   Hi,   
      
   Have a look at the following code:   
      
   class A   
   {   
   public:   
        A() {}   
        A(const A& that) {}   
        A(A&& that) {}   
   };   
      
   template    
   void f(Type&& that)   
   {   
        A b(std::move(that));   
   }   
      
   int main()   
   {   
        A a;   
        f(a); // Compiles.   
      
        return 0;   
   }   
      
   It is my understanding that 'b' gets move-constructed from 'a' in f(a):   
      
     * 'a' is an lvalue, and so Type = A& in f(a).   
     * By the reference collapsing rules, A& && = A&.   
     * Thus the type of 'that' is A&.   
     * 'that' is an lvalue, but gets converted to an rvalue by std::move.   
     * Therefore, 'b' gets move-constructed from 'a'.   
      
   Now consider how close this is to the case banned by the standard, that   
   rvalue references can not bind to lvalue references:   
      
   void g(A&& a)   
   {   
        A b(std::move(a));   
   }   
      
   A a;   
   g(a); // Error.   
      
   This is for a good reason, of course, because we usually have further   
   use for an lvalue afterwards.   
      
   I am feeling unease about the template moving from an lvalue as   
   something that might cause potential traps. What do you think of it?   
      
   --   
   http://kaba.hilvi.org   
      
      
         [ 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)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca