Chapter 5. Rvalue References, Move Semantics, and Perfect Forwarding Flashcards

1
Q

Item 23: Understand std::move and std::forward.

A

std: :move performs an unconditional cast to an rvalue. In and of itself, it doesn’t move anything.
std: :forward casts its argument to an rvalue only if that argument is bound to an rvalue.

Neither std::move nor std::forward do anything at runtime.

Move requests on const objects are treated as copy requests.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Item 24: Distinguish universal references from rvalue references.

A

If a function template parameter has type T&& for a deduced type T, or if an object is declared using auto&&, the parameter or object is a universal reference.

If the form of the type declaration isn’t precisely type&&, or if type deduction does not occur, type&& denotes an rvalue reference.

Universal references correspond to rvalue references if they’re initialized with rvalues. They correspond to lvalue references if they’re initialized with lvalues.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Item 25: Use std::move on rvalue references, std::forward on universal references.

A

Apply std::move to rvalue references and std::forward to universal references the last time each is used.

Do the same thing for rvalue references and universal references being returned from functions that return by value.

Never apply std::move or std::forward to local objects if they would otherwise be eligible for the return value optimization.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Item 26: Avoid overloading on universal references.

A

Overloading on universal references almost always leads to the universal reference overload being called more frequently than expected.

Perfect-forwarding constructors are especially problematic, because they’re typically better matches than copy constructors for non-const lvalues, and they can hijack derived class calls to base class copy and move constructors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Item 27: Familiarize yourself with alternatives to overloading on universal references.

A

Alternatives to the combination of universal references and overloading include the use of distinct function names, passing parameters by lvalue-reference-to-const, passing parameters by value, and using tag dispatch.

Constraining templates via std::enable_if permits the use of universal references and overloading together, but it controls the conditions under which compilers may use the universal reference overloads.

Universal reference parameters often have efficiency advantages, but they typically have usability disadvantages.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Item 28: Understand reference collapsing.

A

Reference collapsing occurs in four contexts: template instantiation, auto type generation, creation and use of typedefs and alias declarations, and decltype.

When compilers generate a reference to a reference in a reference collapsing context, the result becomes a single reference. If either of the original references is an lvalue reference, the result is an lvalue reference. Otherwise it’s an rvalue reference.

Universal references are rvalue references in contexts where type deduction distinguishes lvalues from rvalues and where reference collapsing occurs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Item 29: Assume that move operations are not present, not cheap, and not used.

A

Assume that move operations are not present, not cheap, and not used.

In code with known types or support for move semantics, there is no need for assumptions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Item 30: Familiarize yourself with perfect forwarding failure cases.

A

Perfect forwarding fails when template type deduction fails or when it deduces the wrong type.

The kinds of arguments that lead to perfect forwarding failure are braced initializers, null pointers expressed as 0 or NULL, declaration-only integral const static data members, template and overloaded function names, and bitfields.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly