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,572 of 33,346    |
|    SG to All    |
|    Re: lvalues, rvalues, temporary values -    |
|    03 Oct 12 09:22:31    |
   
   From: s.gesemann@googlemail.com   
      
   { Please limit your text to fit within 80 columns, preferably around 70,   
    so that readers don't have to scroll horizontally to read each line.   
    This article has been reformatted manually by the moderator. -mod }   
      
   Am Mittwoch, 3. Oktober 2012 01:23:08 UTC+2 schrieb Simon Parmenter:   
   > Hi All   
   >   
   > I was just playing around with some test code I had done sometime ago and   
   > found some behaviour I could not explain; looking up the C++11 standard   
   > did me no good either.   
      
   You're invoking undefined behaviour. Simple as that. See my comments   
   below.   
      
   > int main()   
   > {   
   > std::string s1("Hello");   
   > std::string s2(" World");   
   > s1 + s2 = "Eh?";   
   > std::string & sr = (s1 + s2 = "Eh?");   
   > std::string const & sr1(s1 + s2);   
   > cout << (s1 + s2 = "Eh?") << endl;   
   > cout << sr << endl;   
   > cout << sr1 << endl;   
   > }   
      
   sr is a dangling reference. You initialized it to refer to a temporary   
   object which vanishes soon after that. The assignment operator just reterns   
   a reference to this object and the compiler loses the information that this   
   is actually a reference to a temporary (because the function signature   
   of operator= doesn't say it). So, the temporary life-time extension rule   
   that applies in the case for sr1 is not applicable to the case sr.   
      
      
   > int main()   
   > {   
   > std::string s1("Hello");   
   > std::string s2(" World");   
   > s1 + s2 = "Eh?";   
   > std::string & sr = (s1 + s2 = "Eh?");   
   > cout << sr << endl;   
      
   Same here. sr is a dangling reference. Accessing it invokes undefined   
   behaviour.   
      
   > std::string const & sr1(s1 + s2);   
      
   while for this line a special life-time extension rule kicks in. s1+s2   
   directly returns a temporary object (and not a reference like the   
   assignment operator) so the rule applies and the temporary's life-time   
   is extended to the life-time of the reference sr1.   
      
   You might want to check out the following article as well:   
   http://herbsutter.com/2008/01/01/gotw-88-a-candidate-for-the-mos   
   -important-const/   
      
      
   > int main()   
   > {   
   > std::string s1("Hello");   
   > std::string s2(" World");   
   > s1 + s2 = "Eh?";   
   > std::string & sr = (s1 + s2 = "Eh?");   
   > cout << sr << endl;   
      
   Once again, undefined behaviour.   
      
      
   > int main()   
   > {   
   > std::string s1{"Hello"};   
   > std::string s2{" World"};   
   > s1 + s2 = "Eh?";   
   > std::string const & sr{s1 + s2 = "Eh?"};   
   > cout << sr << endl;   
      
   Once again, undefined behaviour.   
      
      
   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