lundi 27 février 2017

How to use sfinae to check, whether type has operator ()?

I have following code:

template <typename T>
struct function_traits
{
    typedef decltype(&T::operator()) test_type;
    typedef std::true_type res_type;
};

template <typename T>
struct function_traits
{
    typedef std::false_type res_type;
};

In other words, I want to know whether type has operator (). I thought that I can use SFINAE way to do this. However compiler tells:

'function_traits' : class template has already defined.

What's wrong with such code?

P.S.: here is simple usage:

auto f1 = [](const int a){ std::cout << a << std::endl; };
function_traits<decltype(f1)>::res_type;
auto f2 = false;
function_traits<decltype(f2)>::res_type;

EDIT: I am using c++ 11 standard

Aucun commentaire:

Enregistrer un commentaire