cechy do testowania, czy func (args) jest dobrze ukształtowany i czy ma wymagany typ powrotu

Istnieje wiele podobnych pytań / odpowiedzi, ale nie mogłem zebrać tych odpowiedzi razem, aby służyć moim celom. Chcę cech

template<typename Func, typename ReturnType, typename... Args>
struct returns_a { static const bool value; };

takie

returns_a<F,T,Args>::value

jest prawdą, jeśliF(Args) jest dobrze uformowany i zwraca aT. Po kilku dalszych badaniach udało mi się to osiągnąć w następujący sposób:

// value is true if Func(Args...) is well formed
template<typename Func, typename... Args>
class is_callable
{
  template <typename F>
  static decltype(std::declval<F>()(std::declval<Args>()...), void(), 0) test(int);
  template <typename>
  static void test(...);
public:
  static const bool value = !std::is_void<decltype(test<Func>(0))>::value;
};

// return_type<FunctionSignature>::type is the return type of the Function
template<typename>
struct return_type {};

template<typename ReturnType, typename... Args>
struct return_type<ReturnType(Args...)>
{ typedef ReturnType type; };

// helper class, required to use SFINAE together with variadic templates parameter
// generic case: Func(Args...) is not well-defined
template <typename Func, typename ReturnType, typename dummy, typename... Args>
struct returns_a_helper { static const bool value = false; };

// Func is a function signature
template <typename Func, typename ReturnType, typename... Args>
struct returns_a_helper<Func, ReturnType, typename
                        std::enable_if<std::is_function<Func>::value>::type, Args...>
{
  static const bool value =
    std::is_convertible<typename return_type<Func>::type,
                        ReturnType>::value;

};

// Func(Args...) not a function call, but well-defined 
template <typename Func, typename ReturnType, typename... Args>
struct returns_a_helper<Func,ReturnType,typename
                        std::enable_if<is_callable<Func>::value &&
                       !std::is_function<Func>::value
                                      >::type, Args...>
{
  static const bool value =
    std::is_convertible<typename std::result_of<Func(Args...)>::type,
                ReturnType>::value;
};

template <typename Func, typename ReturnType, typename... Args>
struct returns_a : returns_a_helper<Func, ReturnType, void, Args...> {};

który teraz działa dobrze dla funktorów i funkcji. Oto prosty test:

struct base { virtual bool member(int) const = 0; };
struct foo : base { bool member(int x) const { return x&2; } };
struct bar { foo operator()() { return foo(); } };
foo free_function() { return foo(); }

template<typename T, typename Func>
void test(Func const&func)
{
   std::cout << std::boolalpha << returns_a<Func,T>::value << std::endl;
}

int main()
{
   foo x;
   bar m;
   test<const base&>([&]() { return x; });
   test<const base&>(m);
   test<const base&>(free_function);
   return 0;
}

Cóż, to działa, ale wydaje się trochę kłopotliwe. Każdy ma lepsze / bardziej eleganckie / krótsze rozwiązania?

questionAnswers(3)

yourAnswerToTheQuestion