Why is Universal reference being treated as R-Value reference?
I thought
T&&
invoid func1(T&& t)
should be deduced as a universal reference or a forwarding reference [...]
No. t
in your example is not a universal reference. To be a universal reference it must be deduced from the call, but in your example T
is just A
. This is a universal reference:
template <class T>
class Test
{
public:
template <typename X>
void func1(X&& t)
{
}
};
The important detail is "deduced". In your example nothing is being deduced. Consider that Test<A>
is equivalent to
class TestA
{
public:
void func1(A&& t)
{
}
};
Once you instantiated the class template, its method func1
has argument of tpye A&&
, not something else, ie there is nothing to be deduced anymore. Template argument deduction takes place when you do not explicitly state the template argument, as in
template <typename T> void foo(T&& t) {};
foo(1);