matzar / moderncpp Goto Github PK
View Code? Open in Web Editor NEWRepository to keep track of my C++ research and learning.
License: MIT License
Repository to keep track of my C++ research and learning.
License: MIT License
http://thbecker.net/articles/rvalue_references/section_01.html
rvalue further reading:
https://www.artima.com/cppsource/rvalue.html
Why implicit move must go:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3153.htm
https://www.justsoftwaresolutions.co.uk/cplusplus/rvalue_references_and_perfect_forwarding.html
http://thbecker.net/articles/rvalue_references/section_05.html
Things that are declared as rvalue reference can be lvalues or rvalues. The distinguishing criterion is: if it has a name, then it is an lvalue. Otherwise, it is an rvalue.
https://www.youtube.com/playlist?list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb&disable_polymer=true
If you implement:
void foo(X&);
but not:
void foo(X&&);
then of course the behavior is unchanged: foo can be called on l-values, but not on r-values.
If you implement:
void foo(X const &);
but not:
void foo(X&&);
then again, the behavior is unchanged:
foo can be called on l-values and r-values, but it is not possible to make it distinguish between l-values and r-values. That is possible only by implementing:
void foo(X&&);
as well. Finally, if you implement:
void foo(X&&);
but neither one of
void foo(X&);
and
void foo(X const &);
then, according to the final version of C++11, foo can be called on r-values, but trying to call it on an l-value will trigger a compile error.
Distinguish between prefix and postfix forms of increment and decrement operators.
https://www.artima.com/cppsource/rvalue.html
http://thbecker.net/articles/rvalue_references/section_07.html
template<typename T, typename Arg>
shared_ptr factory(Arg const & arg)
{
return shared_ptr(new T(arg));
}
There are two problems with this approach. Firstly, if factory had not one, but several arguments, you would have to provide overloads for all combinations of non-const and const reference for the various arguments. Thus, the solution scales extremely poorly to functions with several arguments.
Secondly, this kind of forwarding is less than perfect because it blocks out move semantics: the argument of the constructor of T in the body of factory is an lvalue. Therefore, move semantics can never happen even if it would without the wrapping function.
It turns out that rvalue references can be used to solve both these problems. They make it possible to achieve truly perfect forwarding without the use of overloads. In order to understand how, we need to look at two more rules for rvalue references.
http://thbecker.net/articles/rvalue_references/section_08.html
Suppose that we call std::move on an lvalue of type X:
X x;
std::move(x);
By the new special template deduction rule, the template argument T will resolve to X&. Therefore, what the compiler ends up instantiating is
typename remove_reference<X&>::type&&
std::move(X& && a) noexcept
{
typedef typename remove_reference<X&>::type&& RvalRef;
return static_cast(a);
}
After evaluating the remove_reference and applying the new reference collapsing rules, this becomes
X&& std::move(X& a) noexcept
{
return static_cast<X&&>(a);
}
That does the job: our lvalue x will bind to the lvalue reference that is the argument type, and the function passes it right through, turning it into an unnamed rvalue reference.
I leave it to you to convince yourself that std::move actually works fine when called on an rvalue. But then you may want to skip that: why would anybody want to call std::move on an rvalue, when its only purpose is to turn things into rvalues? Also, you have probably noticed by now that instead of
std::move(x);
you could just as well write
static_cast<X&&>(x);
However, std::move is strongly preferred because it is more expressive.
http://thbecker.net/articles/rvalue_references/section_06.html
Consider the following function definition:
X foo()
{
X x;
// perhaps do something to x
return x;
}
Now suppose that as before, X is a class for which we have overloaded the copy constructor and copy assignment operator to implement move semantics. If you take the function definition above at face value, you may be tempted to say, wait a minute, there is a value copy happening here from x to the location of foo's return value.
Let me make sure we're using move semantics instead:
X foo()
{
X x;
// perhaps do something to x
return std::move(x); // making it worse!
}
Unfortunately, that would make things worse rather than better. Any modern compiler will apply return value optimization to the original function definition. In other words, rather than constructing an X locally and then copying it out, the compiler would construct the X object directly at the location of foo's return value. Rather obviously, that's even better than move semantics.
So as you can see, in order to really use rvalue references and move semantics in an optimal way, you need to fully understand and take into account today's compilers’ “special effects” such as return value optimization and copy elision. For a detailed discussion see Items 25 and 41 of Scott Meyers' book “Effective Modern C++”. It gets get pretty subtle, but hey, we chose C++ as our language of choice for a reason, right? We made our beds, so now let's lie in them.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.