本文介绍了traits用于测试func(args)是否格式良好并且需要返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 有许多类似的问题/答案,但我不能把这些答案在一起为我的目的服务。我想要一个traits There are a number of similar questions/answers, but I couldn't quite put those answers together to serve my purposes. I want a traitstemplate<typename Func, typename ReturnType, typename... Args>struct returns_a { static const bool value; };以便returns_a<F,T,Args>::value c $ c> F(Args)形成良好,并返回 T 。经过一些更多的研究,我得到它的工作如下:is true if F(Args) is well formed and returns a T. After some more research, I got it working as follows:// value is true if Func(Args...) is well formedtemplate<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 Functiontemplate<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-definedtemplate <typename Func, typename ReturnType, typename dummy, typename... Args>struct returns_a_helper { static const bool value = false; };// Func is a function signaturetemplate <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...> {};现在可以正常工作和函数。这里是一个简单的测试:which now works fine for functors and functions. Here is a simple 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;}好吧,这个工作,但看起来有点麻烦。 Well, this works, but it seems a bit cumbersome. Anybody has better/more elegant/shorter solutions?推荐答案这是很尴尬的:我发现一个相当简单方法: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...)>>; 。 这篇关于traits用于测试func(args)是否格式良好并且需要返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-01 05:46