I thought universal reference (T&&) is supposed to take any kind of reference. But the following doesn't work.
I run into this problem when I try to be const-correct in a library that I am writing. I am new to C++ and haven't seen something like this before.
test.cpp:
enum Cv_qualifier {
constant,
non_const
};
template <Cv_qualifier Cv> class A;
template<>
class A<Cv_qualifier::constant> {
public:
template<Cv_qualifier Cv2>
void t(const A<Cv2>&& out) {}
};
template <>
class A<Cv_qualifier::non_const> {
public:
template<Cv_qualifier Cv2>
void t(const A<Cv2>&& out) {}
};
int main()
{
A<Cv_qualifier::non_const> a;
A<Cv_qualifier::constant> b;
a.t(b);
}
Error (compiled with g++ test.cpp -std=c++11):
test.cpp: In function ‘int main()’:
test.cpp:24:10: error: cannot bind ‘A<(Cv_qualifier)0u>’ lvalue to ‘const A<(Cv_qualifier)0u>&&’
a.t(b);
^
test.cpp:17:10: note: initializing argument 1 of ‘void A<(Cv_qualifier)1u>::t(const A<Cv2>&&) [with Cv_qualifier Cv2 = (Cv_qualifier)0u]’
void t(const A<Cv2>&& out) {}
^
By the way, in the actual program, the class A does not own any actual data, and contain references to another class that actually hold the data. I hope this means I am not constantly create indirection/copy data when I allow the member function t of class A to accept temporary objects.
Universal reference, or forwarding reference, only happen because of reference collapsing. It work that way:
That way, when you receive
T&&in a template function, the rvalue reference can collapse to other types of reference depending of the type ofT. In any other cases, when the collapsing don't happen,SomeType&&will staySomeType&&and will be an rvalue reference.With that said, if you want your function to support forwarding, you can do that:
Indeed, now the collapsing happen. If you want to extract the
Cv_qualifiervalue fromT, you can make yourself a type trait that do that:Then, inside your function
t, you can do that:If you can't use C++17's
std::void_t, you can implement it like that:However, if you only want to test if
Tis anA<...>, use this:Don't forget, to use it with
std::decay_t: