Rvalue reference usage in sample is_copy_asignable implementation

45 Views Asked by At

I am watching Walter Brown's talk on CppCon 2014 'Modern Template Metaprogramming: A Compendium'. In part two, he demonstrates the usage of decltype and declval with a sample implementation of is_copy_assignable.

Here is a link to the video, with the slide, showing the source code (rewind a bit if you want to hear his explanations): https://youtu.be/a0FliKwcwXE?t=1576

Here I have typed his implementation in a sample program:

#include <iostream>
#include <type_traits>
#include <mutex>

using namespace std;


template <class T>
struct my_is_copy_assignable {
private:
    template<class U, class = decltype(declval<U&>() = declval<U const&>())>
    static true_type try_assignment(U&&);   // Why U&& and not for example U?

    static false_type try_assignment(...);

public:
    using type = decltype( try_assignment( declval<T>() ) );
};


int main()
{
    cout << "Is mutex copy assignable: " 
         << my_is_copy_assignable<mutex>::type() << endl
         << "Is int copy assignable: " 
         << my_is_copy_assignable<int>::type() << endl;

    return 0;
}

My question is about the function template (the commented one):

static true_type try_assignment(U&&);

Why he is using rvalue reference? I tried it with lvalue reference and even pass-by-value parameter and it seems to work.

What I am missing here?

0

There are 0 best solutions below