Difference between revisions of "Intermediate C++ Game Programming Tutorial 20"
(→Video Timestamp Index) |
(→Video Timestamp Index) |
||
Line 32: | Line 32: | ||
</div> | </div> | ||
* Overloading a function to take an rvalue reference using <code>&&</code> [https://youtu.be/93mlQnWbyuk?t=6m11s 6:11] | * Overloading a function to take an rvalue reference using <code>&&</code> [https://youtu.be/93mlQnWbyuk?t=6m11s 6:11] | ||
+ | <div class="mw-collapsible-content"> | ||
** Using the <code>&&</code> declarator in a function signature, e.g. <code>void SomeFunc(SomeType&& donor)</code> | ** Using the <code>&&</code> declarator in a function signature, e.g. <code>void SomeFunc(SomeType&& donor)</code> | ||
** This makes the compiler dispatch a function call to the overloaded version when an rvalue is passed <br />(note: <code>&&</code> was added in c++11; <code>&</code> is the familiar lvalue reference declarator) | ** This makes the compiler dispatch a function call to the overloaded version when an rvalue is passed <br />(note: <code>&&</code> was added in c++11; <code>&</code> is the familiar lvalue reference declarator) | ||
+ | </div> | ||
* Converting a variable into an rvalue reference using <code>static_cast<Type&&>(var)</code> [https://youtu.be/93mlQnWbyuk?t=8m00s 8:00] | * Converting a variable into an rvalue reference using <code>static_cast<Type&&>(var)</code> [https://youtu.be/93mlQnWbyuk?t=8m00s 8:00] | ||
* WORK-IN-PROGRESS | * WORK-IN-PROGRESS |
Revision as of 05:06, 20 October 2019
In this video we learn about r-value reference and move semantics, which is perhaps the most important feature that was added in the C++11 update. This is going to allow us to manage and transfer our resources in a precise and efficient manner. It is sexy as fuck and I love it.
Topics Covered
- r-values and l-values
- r-value reference function overloading
- Move constructor and move assigment
- Rule of 5
-
std::move
-
std::make_move_iterator
Video Timestamp Index
- Why are move semantics important and required? 0:23
- Example use case: adding objects to a container using
emplace_back
- Instead of deep copying every element of the container when expanding the container capacity, move semantics allow you to direct pointers in the new container to existing blocks of memory
- Example use case: adding objects to a container using
- What is an rvalue (simple definition) 2:48?
- rvalues are values that can only appear on the right hand side of an assignment operation (=), such as:
- - literals;
69 = 420;
is invalid - - temporary return values of functions;
f() = 420;
, where the signature off
isint f();
, is invalid
- lvalues are values that can appear on the left hand side of assignments; they can be assigned to, for example:
- -
x = 10;
ifx
has been declared asint x;
- -
obj.Get() = 69;
if the member functionGet()
returns a reference to an integer member variable - -
arr[3] = 10;
ifarr
has been declared as an array of integers
- Why is the distinction between rvalues and lvalues useful? 4:57
- The ability to detect whether a function is called with an rvalue (rather than with an lvalue) is useful, because the function can freely mangle an rvalue as it will be destoyed after the function call anyway
- Overloading a function to take an rvalue reference using
&&
6:11
- Using the
&&
declarator in a function signature, e.g.void SomeFunc(SomeType&& donor)
- This makes the compiler dispatch a function call to the overloaded version when an rvalue is passed
(note:&&
was added in c++11;&
is the familiar lvalue reference declarator)
- Using the
- Converting a variable into an rvalue reference using
static_cast<Type&&>(var)
8:00 - WORK-IN-PROGRESS
Note
If you are using Visual Studio 2017, you might notice that our move members are not being used during std::vector
growth even after we have 'properly' implemented them. This is expected, so don't pay it much mind at the moment.
The reason for this is that std::vector
will only use the move members if they are guaranteed not to throw any exceptions (as per the standard). This issue will be dealt with in Intermediate 22 (tutorial on exceptions). For your interest, you can enable the move optimization by declaring your move ctor as Surface( Surface&& ) noexcept
. The same should be done for the move assignment and the destructor.
You might wonder why Chili had no problems in the video. In the video, Chili was using Visual Studio 2015, which does not conform to the standard in this point, so it uses the move members regardless of whether or not they are marked noexcept
.