When are rvalue references to primitive integers short-lived or long-lived?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: When are rvalue references to primitive integers short-lived or long-lived?

  1. #1
    Join Date
    Jun 2012
    Location
    Dhaka
    Posts
    4

    When are rvalue references to primitive integers short-lived or long-lived?

    I have done some experimentation on rvalue references with the TDM-GCC 4.6.1 compiler and made some interesting observations that I cannot explain away with theories. I would like experts out there to help me explain them.

    I have a very simple program that does not deal with objects but int primitives and that has defined 2 functions:
    foo1 (returning a local variable by rvalue reference) and
    foo2 (returning a local variable by value)

    #include <iostream>

    using namespace std;

    int &&foo1();
    int foo2();

    int main()
    {

    int&& variable1 = foo1();
    //cout << "My name is softwarelover." << endl;
    cout << "variable1 is: " << variable1 << endl; // Prints 5.
    cout << "variable1 is: " << variable1 << endl; // Prints 0.

    int&& variable2 = foo2();
    cout << "variable2 is: " << variable2 << endl; // Prints 5.
    cout << "variable2 is still: " << variable2 << endl; // Still prints 5!

    return 0;
    }

    int &&foo1() {

    int a = 5;
    return static_cast<int&&>(a);
    }

    int foo2() {

    int a = 5;
    return a;
    }


    It seems the value returned by foo1 and received by variable1 dies out after some time - perhaps, a brief period of some milliseconds. Notice that I have prevented cout from printing "My name is softwarelover" by commenting it out. If I allow that statement to run, the result is different. Instead of printing 5, 0 it prints 0, 0. Seems like it is because of the time-delay introduced by "cout << "My name is softwarelover." that 5 turns into 0.

    Is the above how an rvalue reference is supposed to behave when referring to a primitive integer which a function returned by reference as opposed to return-by-value? By the way, why is it 0, why not garbage?

    Notice also that variable2 never seems to die out, no matter how many times I print it with cout! variable2 refers to a primitive integer which a function returned by value, not return-by-reference.

    Thanks.

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,632

    Re: When are rvalue references to primitive integers short-lived or long-lived?

    This isn't an easy subject so for what's it worth...

    I browsed the standard and in 3.10 I found this
    An xvalue (an eXpiring value) also refers to an object, usually near the end of its lifetime (so that its
    resources may be moved, for example). An xvalue is the result of certain kinds of expressions involving
    rvalue references (8.3.2). [ Example: The result of calling a function whose return type is an rvalue
    reference is an xvalue. end example ]
    My interpretation of it is that you in order to not jeopardize the returned value you have to immediately assign it to something persistent. Failure to do so invokes undefined behaviour (didn't find anything explicit to support that though) so when using another compiler, other compiler flags or whatever there will be another result.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  3. #3
    Join Date
    Oct 2008
    Posts
    1,116

    Re: When are rvalue references to primitive integers short-lived or long-lived?

    Quote Originally Posted by S_M_A
    My interpretation of it is that you in order to not jeopardize the returned value you have to immediately assign it to something persistent. Failure to do so invokes undefined behaviour
    no, it's not undefined behavior per se; it's just that, semantically, xvalues refers to objects from which you can safely steal resources from ( for example, you can turn any lvalue in an xvalue via std::move, which is just a "polite" cast to an rvalue reference ).

    that said, the OP code snippet does give undefined behavior, but for the simple reason that he's using a reference (&& or & it would be the same ) to an object that does not exist anymore ( the automatic variable "int a" in foo1() ).

    Quote Originally Posted by softwarelover
    It seems the value returned by foo1 and received by variable1 dies out after some time - perhaps, a brief period of some milliseconds.
    no, this is just a manifestation of undefined behavior.

    Quote Originally Posted by softwarelover
    Notice also that variable2 never seems to die out, no matter how many times I print it with cout!
    in this case the temporary is bound to the reference, as in "int const& variable3 = foo2()". So it's legal and expected behavior.

    EDIT: BTW, is it me, or the forum is nearly unusable today ?? it took me ages to edit this damned post ...
    Last edited by superbonzo; September 5th, 2012 at 02:19 AM.

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,632

    Re: When are rvalue references to primitive integers short-lived or long-lived?

    In my mind we say about the same thing superbonzo, using a reference to an object that doesn't exist anymore give undefined behaviour (maybe that's also what you ment and it's just me not having english as my native language...)

    Isn't the thing here though that the returned rvalue reference kind of imply that a move operation has to be executed immediately?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  5. #5
    Join Date
    Oct 2008
    Posts
    1,116

    Re: When are rvalue references to primitive integers short-lived or long-lived?

    Quote Originally Posted by S_M_A View Post
    In my mind we say about the same thing
    ... uhm, I think not: you quoted the definition of xvalue relating it to the return value of the foo1 function; this is quite wrong as the problem here has nothing to do with rvalue references, nor the lifetime of return values. Instead, the problem lies inside the foo1 function, when a reference to an automatic variable is returned.

    Quote Originally Posted by S_M_A View Post
    Isn't the thing here though that the returned rvalue reference kind of imply that a move operation has to be executed immediately?
    what do you mean ? rvalue references are just references, they do not imply any "move operation" per se.

    Of course, the semantics of rvalues is such that when they bind to a function argument then that function is expected to possibly steal its resources, treating it as a to-be-moved temporary ( the standard explictly says this; for example, contrary to ordinary references, function implementations are allowed to ignore aliasing checks ). But it's function implementation specific whether or when any move operation actually occurs, and it's class implementation specific how such a move is performed ( note that for non-class types, "move-operations" are just (reference) conversions/initializations ).

  6. #6
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,632

    Re: When are rvalue references to primitive integers short-lived or long-lived?

    Hm, seems I've gotten this totally wrong. More reading to do...
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  7. #7
    Join Date
    Feb 2013
    Posts
    4

    Re: When are rvalue references to primitive integers short-lived or long-lived?

    Cho thue kho hoai duc, cho thuê kho Ho*i D?c, HN

    Di?n t*ch: Hon 2000 m2 (kho) v* 370 m2 x 2 t?ng (nh* di?u h*nh)

    H? th?ng kho v* xu?ng n?m trong KCN, xu?ng d?p, cao, xe container ra v*o r?t thu?n ti?n, b?o v? 24/24, r?t thu?n l?i cho vi?c l*m xu?ng s?n xu?t, l*m kho h*ng, m? trung tâm d*o t?o v* d?y ngh?.

    Gi?y t? d?y d?, xu?t du?c hóa don. Di?n 3 pha, nu?c d?y d?.

    Cho thue kho uu tiên cho thuê lâu d*i.

    Quý khách có nhu c?u vui lòng liên h?:

    Ms Dung: 0462733819

    Email: dungkmass@gmail.com

    Công ty CP Tu v?n BDS Kmass Vi?t Nam

    P 213, 51 D?c Ng?, Ba Dình, H* N?i, VN

    http://kmassproperty.com

    http://batdongsankmass.com

  8. #8
    Join Date
    Feb 2013
    Posts
    4

    Re: When are rvalue references to primitive integers short-lived or long-lived?

    Cho thuê kho t?i qu?n Long Biên, kho d?ng m?i, cao, thoáng

    Di?n t*ch cho thue kho t? 500 m2 d?n 1000 m2

    Giá cho thue kho t? 62.000 vnd/m2/tháng d?n 65.000 vnd/m2/tháng

    H?p d?ng cho thue kho t? 1 nam d?n 05 nam

    H? th?ng di?n 3 pha, nu?c s?ch v* xe công 40 feet có th? v*o du?c.

    D? cao kho cho thue t? 5 mét d?n 12 mét.

    Cho thue kho d?c l?p ho?c cho thue kho chung.

    Quý khách có nhu c?u vui lòng liên h?:

    Ms Dung: 0462733819

    Email: dungkmass@gmail.com

    Công ty CP Tu v?n BDS Kmass Vi?t Nam

    P 213, 51 D?c Ng?, Ba Dình, H* N?i, VN

    http://kmassproperty.com

    http://batdongsankmass.com

  9. #9
    Join Date
    Feb 2013
    Posts
    4

    Re: When are rvalue references to primitive integers short-lived or long-lived?

    Cho thuê kho, nh* xu?ng 900m2 Huy?n T? Liêm

    Di?n t*ch: 900m2

    Giá H?p lý

    H?p d?ng 5 nam

    Quý khách có nhu c?u vui lòng liên h?:

    Ms Dung: 0462733819

    Email: dungkmass@gmail.com

    Công ty CP Tu v?n BDS Kmass Vi?t Nam

    P 213, 51 D?c Ng?, Ba Dình, H* N?i, VN

    http://kmassproperty.com

    http://batdongsankmass.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center