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.
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 -
- 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: