T
Thomas Becker
thbecker.net · Personal blog · on HN since 2012
Thomas Becker writes C++ and JavaScript tutorials, best known for his article explaining C++ rvalue references.
What HN saysHN links it as the standard explanation of rvalue references, move semantics and perfect forwarding; the author notes it ranks first in Google for the topic.
15HN people shared it
9linked it in comments
9stories submitted
32story points
3 years agolast shared
2012people per year2026
Most-shared posts
- 1C++ Rvalue References Explained11 people · 13 points · first shared 2012 · discussion on HN
- 2Section 082 people · first shared 2015
- 3OOP in JavaScript: A Tutorial for Those Who Are Accustomed to Classes1 people · 13 points · first shared 2014 · 3 comments on HN
- 4I didn't know that C++ auto and decltype were this tricky1 people · 3 points · first shared 2013 · discussion on HN
- 5Electronic Book Readers vs. Paper and Ink1 people · 3 points · first shared 2015 · discussion on HN
- 6Section 031 people · first shared 2013
- 7Page 011 people · first shared 2015
- 8Section 071 people · first shared 2015
What HN people wrote when they linked it
thebear · 2018-01-31
Author here. A few years back, I wrote an article about the issues that you're asking about. Since the article still comes up as the #1 result of Google search for "C++ rvalue references", I believe it's ok for me to recommend it here:
http://thbecker.net/articles/rvalue_references/section_01.html
Warning: Read only if you have a serious interest in C++.
throwawayaway · 2015-05-17
thanks for the recommendation.
i think malloc and free are a lesser evil than this stuff:
http://thbecker.net/articles/rvalue_references/section_08.html
feel free to disagree!
ecosse · 2013-08-01
Worth noting that int&& is not illegal in C++11 since it specifies an rvalue reference, essentially for efficient move operations. http://thbecker.net/articles/rvalue_references/section_03.html
Arech · 2023-07-13
I think you're talking about perfect forwarding and reference collapsing rules [1, 2], introduced in C++11, but that is a bit different thing occurring only in a template instantiation context. Normally, you wouldn't see triple ampersands in sane C++ code, afaik.
[1] http://thbecker.net/articles/rvalue_references/section_08.html
[2] https://en.cppreference.com/w/cpp/language/reference
pmelendez · 2015-05-17
The link that you provided does not have to do with malloc and/or free but with perfect forwarding:
http://thbecker.net/articles/rvalue_references/section_07.html
Also that example uses a Factory pattern which is actually verbosing the example, but it's like comparing apple and oranges since that's OOP which you actually wouldn't be doing in C.
What you would be comparing is something like:
mystruct * stcVar = malloc(sizeof(mystruct));
free (stcVar);
Vs.
std:shared_ptr<mystruct> stcVar;
Of course that is a stupid example; However things gets more interesting when you have pieces of code sharing the same structure (threads maybe?) and you don't know exactly which code should be the one in charge of releasing the pointer.
stinos · 2014-08-24
I've looked at rvalue reference, and it seems to return an assignable pointer to do other thing from a function
I'm not sure how you came to that conclusion, but it is not correct. R-values don't have much to do with pointers at all. Though they sometimes do solve problems you would be tempted to use pointers for. I admit I do find it hard to explain briefly what they are though. Maybe these are helpful, though it might be hard to grasp if you don't already properly understand C++:
http://www.artima.com/cppsource/rvalue.html
http://thbecker.net/articles/rvalue_references/section_01.html. This example basically gives an idea of why they are useful: http://thisthread.blogspot.be/2011/03/what-is-rvalue-reference-good-fo…
tuhdo · 2014-08-24
Well currently I only work with C not C++ anymore so my knowledge might be outdated. I thought that the rvalue reference is something new in C++11, as I read it here: http://thbecker.net/articles/rvalue_references/section_01.html. Your article seems to mention different thing from 2008? In the article I read, a function returns a pointer that you can assign it like this: get_pointer() = 1;
rsp1984 · 2020-02-17
C++ “move” semantics are simple, but they are still widely misunderstood.
No, they aren't simple and the fact that are still widely misunderstood is basically proof of that.
Maybe, just maybe, it has something to do with stuffing rvalue references, perfect forwarding and the whole universal-references-template-clusterfuck into one and the same syntax. [1]
The default compiler-generated move can leave behind a null sp member
Which is precisely why std::move should be used with caution, which, in turn, is precisely why many codebases are still avoiding it. This problem in particular could have been avoided by not auto-generating move constructors.
I really want to like this feature of C++, but I feel its design is just so horribly bad and confusing that I'd feel like a total d..k if I started to force it on a team of developers (of various levels of experience) if there's no crystal clear need for it (and let's be honest, C++ was already pretty successful in getting shit done before there was std::move and rvalue references).
[1] http://thbecker.net/articles/rvalue_references/section_01.html
shmerl · 2014-08-18
C++11 has some tricky changes which are way not obvious at the first glance. A few useful resources I found:
* http://thbecker.net/articles/rvalue_references/section_01.html
* https://en.wikipedia.org/wiki/Copy_elision
* http://en.cppreference.com/w/cpp/language/eval_order
* https://akrzemi1.wordpress.com
* https://dl.dropboxusercontent.com/u/13100941/C%2B%2B11.pdf
Understanding the combination of copy elision and move semantics (when and how they are applied) can require one to read the standard.
C++11 is not simpler than older C++, but using it is easier, if you have a good understanding of it.