Cannot bind lvalue to A<Cv2>&&
Asked Answered
D

1

6

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.

Dryden answered 15/11, 2016 at 20:50 Comment(9)
You are not using universal references. && is not a universal reference, it's an rvalue reference. T&& is when T is a template parameter deduced by the type it's called with. In other words, T has to be a template parameter and a parameter to the function AND T has to be deduced, not specified.Collusion
So something like std::vector<T>&& or class_name<Template parameters>&& are not universal references? And the only way to write a universal reference is T&&? I thought the Cv_2 template parameter of the t member function has to be deduced.Dryden
If I want an argument that can accept both l-value and r-value reference to a specific class instead of kinds of classes. Is that possible?Dryden
@rxu You can use SFINAE to limit what T can be deduced as.Fornication
Thanks a lot. I was trying to figure this out for a whole day. So T&& is the only way to make an argument that accepts both l-value and r-value references.Dryden
"So something like std::vector<T>&& or class_name<Template parameters>&& are not universal references?" You are right. Universal reference only happen when the compiler can do reference collapsing.Tabling
No, T&& is not the only way for a function to accept l-value and r-values. In your case all you need to do is accept A<Cv2> const& and it'll accept anything.Collusion
"universal reference" only occurs when the parameter is exactly T&& ,and T is a template parameter of the functionOutlaw
@Crazy Eddie: would a parameter A<Cv2> const& a prevent modification of the values of a? ok I get it. So it does limit modification of the values of a. it is a constant reference. #3695130Dryden
T
6

Universal reference, or forwarding reference, only happen because of reference collapsing. It work that way:

T&& & -> T&
T& && -> T&
T&& && -> T&&

That way, when you receive T&& in a template function, the rvalue reference can collapse to other types of reference depending of the type of T. In any other cases, when the collapsing don't happen, SomeType&& will stay SomeType&& and will be an rvalue reference.

With that said, if you want your function to support forwarding, you can do that:

template <Cv_qualifier Cv> struct A;

template<>
struct A<Cv_qualifier::constant> {
    template<typename T> 
    void t(T&& out) {}
};

template <>
struct A<Cv_qualifier::non_const> {
    template<typename T> 
    void t(T&& out) {}
};

Indeed, now the collapsing happen. If you want to extract the Cv_qualifier value from T, you can make yourself a type trait that do that:

template<typename>
struct CvValue;

template<Cv_qualifier cv>
struct CvValue<A<cv>> {
    constexpr static Cv_qualifier value = cv;
};

Then, inside your function t, you can do that:

//                   v----- This is a good practice to apply a constraint
template<typename T, std::void_t<decltype(CvValue<std::decay_t<T>>::value)>* = 0> 
auto t(T&& out) {
    constexpr auto cv = CvValue<std::decay_t<T>>::value;

    // do whatever you want with cv
}

If you can't use C++17's std::void_t, you can implement it like that:

template<typename...>
using void_t = void;

However, if you only want to test if T is an A<...>, use this:

template<typename>
struct is_A : std::false_type {};

template<Cv_qualifier cv>
struct is_A<A<cv>> : std::true_type {};

Don't forget, to use it with std::decay_t:

template<typename T, std::enable_if_t<std::is_A<std::decay_t<T>>::value>* = 0> 
void t(T&& out) {}
Tabling answered 15/11, 2016 at 21:13 Comment(4)
Thanks a lot for the answer! It really makes the universal reference thing more clear. For now, I am trying to use a public typedef within class A to make A::Cv_ reports the Cv template parameter or A. I also try to write a is_A trait to test if a typename is a class A.Dryden
To report the template parameter in A you must not use a typedef, but a constant declared as such: constexpr static Cv_qualifier = Cv;, much like my CvValue I'll update my answer to implement the is_A trait.Tabling
I seem to need struct is_A<A<cv>& > : std::true_type {}; as well. It works now : )Dryden
That's because you missed a std::decay_t. T may resolve to different types of reference, but the type trait don't know about references. You need to use is_A<std::decay_t<T>>::valueTabling

© 2022 - 2024 — McMap. All rights reserved.