Thomas Becker writes C++ and JavaScript tutorials, best known for his article explaining C++ rvalue references.
HN 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.
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
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.
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-for.html
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.
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;
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