I have a problem in my code
Here is simplified version of it :
#include <iostream>
class A
{
public :
template <class T>
void func(T&&)//accept rvalue
{
std::cout<<"in rvalue\n";
}
template <class T>
void func(const T&)//accept lvalue
{
std::cout<<"in lvalue\n";
}
};
int main()
{
A a;
double n=3;
a.func(n);
a.func(5);
}
I expect the output to be :
in lvalue
in rvalue
but it is
in rvalue
in rvalue
why ?!
I think the surprise comes from the way the template argument are deduced. You'll get what you expect if you write:
template <class T> void func(T&&)
isuniversal referenceforwarding reference.To test what you want, try: (Live example)
To build on Jarod42's fine answer, if you want to keep the design of having a primary function template, you can decide based on the deduced type of the universal reference parameter: