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,571 of 33,346   
   SG to All   
   Re: rvalue references and parameter pass   
   03 Oct 12 09:20:10   
   
   From: s.gesemann@googlemail.com   
      
   Am Mittwoch, 3. Oktober 2012 01:23:30 UTC+2 schrieb fmatthew5876:   
   > Suppose I have this:   
   >   
   >  class Matrix4 {   
   >   public:   
   >    //stuff   
   >   private:   
   >    float _m[16];   
   >  };   
   >   
   > Matrix4 operator+(Matrix4 l, const Matrix4& r) {   
   >  l += r;   
   >  return l;   
   > }   
   >   
   > Is there any possible scenario where I would want to also create   
   > a version of operator+ that uses rvalue references? i.e.   
   >   
   > Matrix4 operator+(Matrix4 l, const Matrix4&& r) {   
   >  l+=r;   
   >  return l;   
   > }   
      
   No, not really -- not with this class.   
      
   > I understand rvalue references are desirable in most cases because we   
   > want to use move constructors instead of making unnecessary copies.   
   > But in this particular case, the matrix class directly contains its   
   > data instead of doing an allocation so there is no move constructor.   
      
   Right.   
      
   > To put it more succinctly, is there ever a case when the first operator+   
   > will create a copy for the second argument where the second operator+   
   > would not?   
      
   No. None that I can think of. Rvalue references don't magically avoid   
   copies. They are just means to hold on to "temporary" objects with write   
   access to them. The magic of movable types happens in their respective   
   move operations (ctor, assignment, etc).   
      
   Cheers!   
   SG   
      
      
   --   
         [ 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