keith keith - 9 days ago 6
C++ Question

C++ meta function that determines if a type is callable for supplied arguments

I am trying to implement a C++ template meta function that determines if a type is callable from the method input arguments.

i.e. for a function

void foo(double, double)
the meta function would return
true
for
callable_t<foo, double, double>
,
true
for
callable_t<foo, int, int>
(due to compiler doing implicit cast) and
false
for anything else such as wrong number of arguments
callable_t<foo, double>
.

My attempt is as follows, however it fails for any function that returns anything other than void and I can't seem to fix it.

I am new to template reprogramming so any help would be appreciated.

#include <iostream>
#include <type_traits>
#include <utility>
#include <functional>

namespace impl
{

template <typename...>
struct callable_args
{
};

template <class F, class Args, class = void>
struct callable : std::false_type
{
};

template <class F, class... Args>
struct callable<F, callable_args<Args...>, std::result_of_t<F(Args...)>> : std::true_type
{
};

}

template <class F, class... Args>
struct callable : impl::callable<F, impl::callable_args<Args...>>
{
};

template <class F, class... Args>
constexpr auto callable_v = callable<F, Args...>::value;


int main()
{
{
using Func = std::function<void()>;
auto result = callable_v<Func>;
std::cout << "test 1 (should be 1) = " << result << std::endl;
}

{
using Func = std::function<void(int)>;
auto result = callable_v<Func, int>;
std::cout << "test 2 (should be 1) = " << result << std::endl;
}

{
using Func = std::function<int(int)>;
auto result = callable_v<Func, int>;
std::cout << "test 3 (should be 1) = " << result << std::endl;
}

std::getchar();

return EXIT_SUCCESS;
}


I am using a compiler that supports C++ 14.

Answer

The shorten use of std::result_of to do what you want could look as follows:

template <class T, class, class... Args>
struct callable: std::false_type {
};

template <class T, class... Args>
struct callable<T, decltype(std::result_of_t<T(Args...)>(), void()), Args...>:std::true_type {
};

template <class F, class... Args>
constexpr auto callable_v = callable<F, void, Args...>::value;

[live demo]

you need to remember that type returned by result_of is always the result type of a function you pass to this trait by type. To let your sfinae work you need a method to change this type to void in every possible situation. You can accomplish it by using the trick with decltype (decltype(std::result_of_t<T(Args...)>(), void())).

Edit:

To elaborate the thread of possible drawbacks of the solution utilizing from comments. The std::result_of_t<T(Args...)> type don't need to be equipped with a default non-parametric constructor and as such the sfinae may cause false negative result of callable_v for function that result in this kind of types. In comments I proposed a workaround for the issue that does not really solve the problem or actually generate a new one:

decltype(std::declval<std::result_of_t<T(Args...)>*>(), void())

Intention of this code was to make sfinae work as in previously proposed solution but in case of non-constructable types to create an easy to construct (I thought) object of pointer to given type... In this reasoning I didn't take into consideration the types that one cannot create a pointer to e.g. references. This one again can be workaround by using some additional wrapper class:

decltype(std::declval<std::tuple<std::result_of_t<T(Args...)>>*>(), void())

or by decaying the result type:

decltype(std::declval<std::decay_t<std::result_of_t<T(Args...)>>*>(), void())

but I think it might not be worth it and maybe use of void_t is actually a more straightforward solution:

template <class...>
struct voider {
    using type = void;
};

template <class... Args>
using void_t = typename voider<Args...>::type;

template <class T, class, class... Args>
struct callable: std::false_type {
};

template <class T, class... Args>
struct callable<T, void_t<std::result_of_t<T(Args...)>>, Args...>:std::true_type {
};

template <class F, class... Args>
constexpr auto callable_v = callable<F, void, Args...>::value;

[live demo]