traits for testing whether func(args) is well-formed and has required return type

偶尔善良 提交于 2019-12-03 16:23:41

I think this will do:-

(Corrected including aschepler's test case)

#include <type_traits>

template<typename Func, typename Ret, typename... Args>
struct returns_a
{   
    template<typename F, typename ...As>
    static constexpr bool test(
        decltype(std::declval<F>()(std::declval<As>()...)) * prt) {
        return std::is_same<Ret *,decltype(prt)>::value;
    }

    template <typename F, typename ...As>
    static constexpr bool test(...) {
        return false; 
    }

    static const bool value = test<Func,Args...>(static_cast<Ret *>(0)); 
};

// Testing...

#include <iostream>

void fn0();
int fn1(int);
int fn2(int,int);
struct cls{};
struct fntor
{
    int operator()(int i) { return 1; }
};
auto lamb0 = [](int i) -> int { return i; };
struct B {}; 
struct D : public B {};
auto lamb1 = []{ return B{}; }; 

int main()
{
    std::cout << returns_a<decltype(fn0),void>::value << std::endl; // 1
    std::cout << returns_a<decltype(fn1),int,int>::value << std::endl; // 1
    std::cout << returns_a<decltype(fn1),int,double>::value << std::endl; // 1
    std::cout << returns_a<decltype(fn1),double,int>::value << std::endl; // 0
    std::cout << returns_a<decltype(fn1),char,int>::value << std::endl; // 0
    std::cout << returns_a<decltype(fn1),unsigned,int>::value << std::endl; // 0
    std::cout << returns_a<decltype(fn2),int,int,int>::value << std::endl; // 1
    std::cout << returns_a<decltype(fn2),int,char,float>::value << std::endl; // 1
    std::cout << returns_a<cls,int,int>::value << std::endl; // 0
    std::cout << returns_a<fntor,int,int>::value << std::endl; // 1
    std::cout << returns_a<decltype(lamb0),int,int>::value << std::endl; // 1
    std::cout << returns_a<double,int,int>::value << std::endl; // 0
    std::cout << returns_a<decltype(lamb1), D>::value << std::endl; //0
    return 0;
}

(Built with clang 3.2, gcc 4.7.2, gcc 4.8.1)

#include <tuple>
#include <utility>

template<typename Func, typename R, typename Args, typename=void>
struct will_return_helper: std::false_type {};

template<typename Func, typename R, typename... Args>
struct will_return_helper<
  Func, R, std::tuple<Args...>,
  typename std::enable_if<
    std::is_same<
      R,
      decltype( std::declval<Func&>()( std::declval<Args>()... ) )
    >::value
  >::type
> : std::true_type {};

template<typename Func, typename R, typename... Args>
struct will_return:
  will_return_helper< typename std::decay<Func>::type, R, std::tuple<Args...> >
{};

#include <iostream>
struct Foo {
  int operator()(double) {return 0;}
};
int main()
{
  std::cout << "1 = "<< will_return< int(), int >::value << "\n";
  std::cout << "1 = "<< will_return< int(*)(), int >::value << "\n";
  std::cout << "0 = "<< will_return< int(*)(), double >::value << "\n";
  std::cout << "1 = "<< will_return< Foo, int, double >::value << "\n";
  std::cout << "1 = "<< will_return< Foo, int, int >::value << "\n";
  std::cout << "0 = "<< will_return< Foo, double, int >::value << "\n";
}

Live example.

A better signature for will_return, in my opinion, would be:

template<typename Func, typename Sig>
struct will_return;

template<typename Func, typename R, typename... Args>
struct will_return<Func, R(Args...)>:
  will_return_helper< typename std::decay<Func>::type, R, std::tuple<Args...> >
{};

which gives you:

  std::cout << "1 = "<< will_return< int(), int() >::value << "\n";

and

  std::cout << "1 = "<< will_return< Foo, int(double) >::value << "\n";

which I think looks prettier.

If you prefer "can be converted" rather than "is the same type", you can change is_same above to is_convertible.

Well, this is embarrassing: I found a rather simple (hence elegant) way:

template <typename Func, typename ReturnType, typename... Args>
using returns_a = std::is_convertible<Func, std::function<ReturnType(Args...)>>;

when all the hard work is done in std::function<>.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!