lundi 27 août 2018

Is there a name for this technique and does the language actually allows it?

I have just discovered the following technique. It looks very close to one of proposed concepts syntax, works perfectly on Clang, GCC and MSVC.

template <typename T, typename = typename std::enable_if<std::is_rvalue_reference<T&&>::value>::type>
using require_rvalue = T&&;

template <typename T>
void foo(require_rvalue<T> val);

I tried to find it with search requests like "sfinae in type alias" and got nothing. Is there a name for this technique and does the language actually allows it?


The full example:

#include <type_traits>

//template <typename T, typename std::enable_if<std::is_rvalue_reference<T&&>::value>::type* = nullptr> // On MSVC in this one SFINAE does not work
template <typename T, typename = typename std::enable_if<std::is_rvalue_reference<T&&>::value>::type>
using require_rvalue = T&&;

template <typename T>
void foo(require_rvalue<T>)
{
}

int main()
{
    int i = 0;
    const int ic = 0;
    foo(i); // fail to compile
    foo(ic);  // fail to compile
    foo(std::move(i)); // ok
    foo(123); // ok
}

Aucun commentaire:

Enregistrer un commentaire