Like Tree1Likes
  • 1 Post By whiteflags

Rvalues: They help, how??

This is a discussion on Rvalues: They help, how?? within the General Discussions forums, part of the Community Boards category; I was reading up on the new C++11 standard, and noticed that it's been consistently a big deal that it ...

  1. #1
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,597

    Rvalues: They help, how??

    I was reading up on the new C++11 standard, and noticed that it's been consistently a big deal that it now supports "rvalue references". But how does this help, exactly?
    The example I've seen so far (in multiple places), is the "better way" of swapping objects. But in all those instances, simply swapping them via pointers would have just as efficient.
    Can anyone think of a _real_ example? Maybe one not so lame? As it stands, it seems like a pretty useless feature to me. But hey, 'splain to me otherwise

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    Quote Originally Posted by Yarin
    The example I've seen so far (in multiple places), is the "better way" of swapping objects. But in all those instances, simply swapping them via pointers would have just as efficient.
    If you want to swap via pointers, you need to work with pointers even when you don't otherwise need to, so your counterexample is lame.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Quote Originally Posted by Yarin View Post
    The example I've seen so far (in multiple places), is the "better way" of swapping objects. But in all those instances, simply swapping them via pointers would have just as efficient.
    Can anyone think of a _real_ example? Maybe one not so lame? As it stands, it seems like a pretty useless feature to me. But hey, 'splain to me otherwise
    There are lots of real examples in this long article about rvalue references.

  4. #4
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,597
    Quote Originally Posted by whiteflags View Post
    There are lots of real examples in this long article about rvalue references.
    So far, this one is better than what I've found. Thanks.

    Quote Originally Posted by laserlight View Post
    If you want to swap via pointers, you need to work with pointers even when you don't otherwise need to, so your counterexample is lame.
    I see your point. Though... what you "need" to do is subjective. You don't "need" to do a lot of different things when programming, but you do it anyway, for what ever reason. Usually, it's cleaner, easier, etc., which is the same with rvalue references. I'm just not catching where that would actually be the case, and it would be much more useful than using a pointer. i.e., why add this new feature, when an existing one already does it just as well?

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Well, I don't know if you've read this far or not, but it implies that a non-copyable object can be used in the STL, because of rvalue references and std::move. For me that is enormous, because if there has ever been an idea that I am more skeptical of, it's smart pointers, and I hated how you had to manage a non-copyable object in a smart pointer just to get what you want.

    And there is the whole section on perfect forwarding, which is important if you believe in design patterns, and in particular, the factory pattern. Rvalue references work out to a lot less code for that.

    There's probably an argument somewhere to be made about how many times you needlessly make temporary objects in C++ and rvalue references mitigate that.
    Mario F. likes this.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    Quote Originally Posted by Yarin
    So far, this one is better than what I've found. Thanks.
    Heh, I actually just wanted to link to that and be done with it, but I figured that as it was the second result on Google for "C++ rvalue reference" for me, you probably already found it.

    Quote Originally Posted by Yarin
    Usually, it's cleaner, easier, etc., which is the same with rvalue references. I'm just not catching where that would actually be the case, and it would be much more useful than using a pointer. i.e., why add this new feature, when an existing one already does it just as well?
    Sorry, but I do not agree with your "just as well" claim even for the example of swapping. Perhaps you should demonstrate with some code. After all, "using a pointer" is ambiguous: are you talking about swapping pointers to the objects that you want to swap or swapping objects by swapping internal pointers?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,597
    Quote Originally Posted by whiteflags View Post
    Well, I don't know if you've read this far or not, but it implies that a non-copyable object can be used in the STL, because of rvalue references and std::move. For me that is enormous, because if there has ever been an idea that I am more skeptical of, it's smart pointers, and I hated how you had to manage a non-copyable object in a smart pointer just to get what you want.
    Dunno, I've never really felt a problem here. And smart pointers seem, to me, contrary to the C++ way of doing things. That's what other languages (who do it better) are for :P
    Quote Originally Posted by whiteflags View Post
    And there is the whole section on perfect forwarding, which is important if you believe in design patterns, and in particular, the factory pattern. Rvalue references work out to a lot less code for that.
    Yep, just got there. I have to admit, I do say some possible advantages for that.
    Quote Originally Posted by whiteflags View Post
    There's probably an argument somewhere to be made about how many times you needlessly make temporary objects in C++ and rvalue references mitigate that.
    Yeah I read about that in other articles. Though, I don't find that argument very convincing, as a good optimizer should be able to take care of that for you in most cases.


    I just found this article, specifically, see the section on the "Bulb Paradox". He makes some interesting points. I suppose one could consider these new features in a similar manner.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    Quote Originally Posted by Yarin
    And smart pointers seem, to me, contrary to the C++ way of doing things.
    What is the C++ way of doing things?
    Heh, I actually just wanted to link to that and be done with it, but I figured that as it was the second result on Google for "C++ rvalue reference" for me, you probably already found it.

    Quote Originally Posted by Yarin
    I don't find that argument very convincing, as a good optimizer should be able to take care of that for you in most cases.
    RVO is not always applicable, e.g., when assignment rather than construction is involved.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rvalues and member functions
    By xeddiex in forum C++ Programming
    Replies: 1
    Last Post: 10-22-2006, 03:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21