The following actions will work for any arity, but accept arbitrary argument types:
template <typename T> struct arity { }; template <typename... Args> struct arity<std::function<Foo(Args...)>> { static const int value = sizeof...(Args); };
If you really want to limit the argument type to functions like Foo(Bar, Bar, ...) , you can do something like this:
template <typename T> struct arity { }; template <typename... Args> struct const_tuple { }; template <> struct const_tuple<> { struct unsupported_function_type { }; }; template <typename... Args> struct const_tuple<Bar, Args...> { typedef typename const_tuple<Args...>::unsupported_function_type unsupported_function_type; }; template <typename... Args> struct arity<std::function<Foo(Args...)>> : public const_tuple<Args...>::unsupported_function_type { static const int value = sizeof...(Args); };
This will give you a compilation error whenever arity is called with an unsupported function type.
Paolo capriotti
source share