Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot bind lvalue to A<Cv2>&&

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.

like image 426
hamster on wheels Avatar asked Nov 15 '16 20:11

hamster on wheels


1 Answers

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) {}
like image 72
Guillaume Racicot Avatar answered Oct 21 '22 10:10

Guillaume Racicot