mercredi 26 septembre 2018

Find out if type is callable

I need to figure out if template argument is a callable object with non-void return value. I've defined the following

template<class T, class U = T>
struct is_callable
{
    constexpr static const bool val = false;
};

template<class T>
struct is_callable<T, std::result_of_t<T()>>
{
    constexpr static const bool val = true;
};

template<class T>
constexpr bool is_func = is_callable<T>::val;

but the following variables all are 'false'

auto lambda = []() {return 3.0; };

auto intIsFunc = is_func<int()>; //false -- ok
auto functionIsFunc = is_func<std::function<int()>>; //false -- wrong
auto lambdaIsFunc = is_func<decltype(lambda)>; //false -- wrong

  1. What is wrong with the code?

  2. How to improve the is_func to return 'true' not only on callable objects, but callable with return type which is constructible (use somewhere std::is_constructible_v<>)?

Aucun commentaire:

Enregistrer un commentaire