Sep 17, 2014

[C++11] decltype(auto&&) for forwarding.

Use decltype on auto&& parameters to std::forward them.
[](auto&& arg)
{
 RunMe(std::forward<decltype(arg)>(arg));
}(42);
Reasoning:
if arg is l-value,
auto&& -> auto&&& -> auto&
decltype(arg) -> auto&
thus:
forward<auto&>(arg) -> l-value reference

if arg is r-value,
auto&& -> auto&&
decltype(arg)  -> auto&&
thus:
forward<auto&&>(arg) -> auto&& && -> auto&& -> r-value reverence

------------
i.e
int a;
int& b;
int&& c;

decltype(a) -> int
decltype(b) -> int&
decltype(c) -> int&&

decltype((a)) -> int& , i.e l-value reference
decltype((b)) -> int&& -> int& , i.e l-value reference of l-value reference
decltype((c)) -> int&&& -> int& , i.e l-value reference of r-value reference

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.