c++ - Why is T&& instantiated as int&? -
can please explain why compiles , why t
end type int&
?
#include <utility> void f(int& r) { ++r; } template <typename fun, typename t> void g(fun fun, t&& t) { fun(std::forward<t>(t)); } int main() { int = 0; g(f, i); }
i see on gcc 4.5.0 20100604 , gdb 7.2-60.2
because of perfect forwarding, when argument p&&
lvalue, p
deduced argument's type plus having &
attached. int & &&
p
being int&
. if argument rvalue p
deduced argument's type, would int&&
argument p
being int
if pass, example 0
directly.
int& &&
collapse int&
(this semantic view - syntactically int& &&
illegal. saying u &&
when u
template parameter or typedef refering type int&
, u&&
still type int&
- i.e 2 references "collapse" 1 lvalue reference). that's why t
has type int&
.
Comments
Post a Comment