I'm trying to check if a functor is compatible with a given set of parameter types and a given return type (i.e., given parameter types can be implicitly converted to actual parameter types and vice versa for the return type). Currently, I use the following code for this:
template<typename T, typename R, template<typename U, typename V> class Comparer> struct check_type { enum {value = Comparer<T, R>::value}; }; template<typename T, typename Return, typename... Args> struct is_functor_compatible { struct base: public T { using T::operator(); std::false_type operator()(...)const; }; enum {value = check_type<decltype(std::declval<base>()(std::declval<Args>()...)), Return, std::is_convertible>::value}; };
check_type<T, V, Comparer> In most cases this works very well, however it will not compile when I test parameterless functors such as struct foo{ int operator()() const;}; , beccause in this case, the two operator() bases are based on uncertainty, which leads to something like this:
error: call of '(is_functor_compatible<foo, void>::base) ()' is ambiguous note: candidates are: note: std::false_type is_functor_compatible<T, Return, Args>::base::operator()(...) const [with T = foo, Return = void, Args = {}, std::false_type = std::integral_constant<bool, false>] note: int foo::operator()() const
So obvoiusly I need another way to test this for parameterless functors. I tried to do a partial specialization of is_functor_compatible for an empty parameter, where I check if the &T::operator() a parameterless function that works more or less. However, this approach obviously fails when the functor under test has multiple operator() .
So my question is, is there a better way to check for existence without operator() parameters and how to do it.
c ++ c ++ 11 templates metaprogramming
Grizzly
source share