Can anyone please explain why this compiles and why does t
end up with 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 i = 0;
g(f, i);
}
I see this on GCC 4.5.0 20100604 and GDB 7.2-60.2
(t))` to `f(t)`. This works because `t` is a name and hence an lvalue. Yes, named rvalue references are lvalues. Confusing at first, I know :) But maybe you should explain in more detail what you really want to *achieve*. "This code won't work" and "writing correct code" does not tell me anything about your actual *goal*.
– fredoverflow Dec 01 '10 at 22:02