C++14 introduces generic lambdas, which use auto in their parameter specifications.
|
|
If we want to perfect-fowrad a parameter x to normalize, we make two changes:
- use universal reference
auto&& - apply
decltypeon parameter to specify the correct type for type argument ofstd::forward
|
|
As a fact of decltype(x):
- if
xis bound to an lvalue, it will yield an lvalue refernece; - if
xis bound to an rvalue, it will yield an rvalue reference
Actually, the result of decltype(x) doesn’t follow the convention of std::forward, where it dictates that the type argument be an lvalue reference to indicate an lvalue and a non-reference to indicate an rvalue.
Thanks to reference-collapsing rule, even though rvalue convention is broken here, the collapsing result is still the same. Say the T in the implementation of std::forward below is instantiated as Widget&&, an rvalue reference type:
|
|
and we get this before reference collapsing:
|
|
After reference collapsing:
|
|
This is exactly what we expect.
Variadic parameters
For more than a single parameter, using following format:
|
|