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.

hamster on wheels
  • 2,771
  • 17
  • 50
  • 3
    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. – Edward Strange Nov 15 '16 at 20:53
  • So something like `std::vector&&` or `class_name – hamster on wheels Nov 15 '16 at 20:57
  • 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? – hamster on wheels Nov 15 '16 at 21:00
  • @rxu You can use SFINAE to limit what T can be deduced as. – NathanOliver Nov 15 '16 at 21:01
  • 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. – hamster on wheels Nov 15 '16 at 21:02
  • *"So something like std::vector&& or class_name – Guillaume Racicot Nov 15 '16 at 21:02
  • 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 const&` and it'll accept anything. – Edward Strange Nov 15 '16 at 21:07
  • "universal reference" only occurs when the parameter is exactly `T&&` ,and `T` is a template parameter of the function – M.M Nov 15 '16 at 21:09
  • @Crazy Eddie: would a parameter `A 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. http://stackoverflow.com/questions/3694630/c-const-reference-before-vs-after-type-specifier – hamster on wheels Nov 15 '16 at 21:10

1 Answers1

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) {}
Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141