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,578 of 33,346    |
|    Dave Harris to Kevin McCarty    |
|    Re: rvalue references and parameter pass    |
|    07 Oct 12 05:57:09    |
   
   22e05073   
   From: brangdon@cix.compulink.co.uk   
      
   kmccarty@googlemail.com (Kevin McCarty) wrote (abridged):   
   > Of course a caller who was unaware of the reference return type   
   > might instead write   
   >   
   > const Matrix4 & result = a + b + c;   
   >   
   > and end up with a dangling reference. Is this where the trouble   
   > comes from in your opinion?   
      
   Yes.   
      
      
   > Is it just too unsafe to do this in real-world code, so that even   
   > in C++ 2011 we can take full advantage of modifying temporary   
   > objects only when their class type is built mainly from   
   > stuff allocated with new[]?   
      
   That sounds reasonable to me.   
      
   In practice, return value optimisations are often sufficient. In this   
   case we could probably write:   
      
    Matrix4 operator+( const Matrix4 &lhs, const Matrix4 &rhs ) {   
    Matrix4 result;   
    result._m[0] = lhs._m[0] + rhs._m[0];   
    // ...   
    return result;   
    }   
      
   and avoid redundant copying.   
      
   -- Dave Harris, Nottingham, UK.   
      
      
   --   
    [ 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