This article is inspired from Item 24 of Effective Modern C++ by Scott Meyers.
Even though Rvalue references, and universal references have the same syntax(&&), they are quite different.
In the above examples, the types are known (not deduced). They are Rvalue references
In the above examples, the types are deduced (using type deduction rules). These are universal references.Universal references can accept both Rvalues and Lvalues, but they must be initialized.
We will now examine some special cases which looks like universal references, but are Rvalue references.
In these examples -
Even though Rvalue references, and universal references have the same syntax(&&), they are quite different.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void(int&& xyz); | |
int&& x = funcA(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename T> | |
funcB(T&& x); | |
auto&& x = y; | |
funcC = [](auto&& zy, auto&& mx) | |
{ | |
cout << zy << ' ' << mx; | |
} |
We will now examine some special cases which looks like universal references, but are Rvalue references.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// might look like universal references, but it's not | |
template<typename T> | |
void funcA(vector<T>&& vec) | |
{ | |
} | |
template<typename T> | |
class Car | |
{ | |
void drive(T&& x) | |
{ | |
} | |
template<typename U> | |
void spin(const U&& y) | |
{ | |
} | |
}; | |
void funcB(const T&& x); | |
void funcC(volatile T&& y); |
In these examples -
- The T in vector<T> of funcA is known, when the vector gets instantiated. Hence T is not type deduced.
- In the drive function, T is known when the class Car gets instantiated. Hence T for drive is not type deduced.
- In the spin function U is const qualified. Hence it is not universal reference.
- In funcB, T is const qualified. Hence it is not universal reference.
- In funcC, T is volatile qualified. Hence it is not universal reference.
Modern (Effective) C++ - Difference between RValue references and Universal references
Reviewed by zeroingTheDot
on
February 14, 2018
Rating:
No comments: