function traits.html
获取函数或成员函数的返回类型,参数类型,参数长度,类类型。ide
函数参数列表推断基于typelist:http://www.cnblogs.com/flytrace/p/3551414.html函数
先看一个普通函数非const的特化:测试
template<typename R, typename... Args> struct function_traits<R (Args...)> { typedef R return_type; typedef typelist<Args...> arglist; enum { arg_count = sizeof...(Args) }; template<unsigned int N> struct arg { typedef typename at<N, arglist>::type type; }; };
使用:spa
int testfunc1(char) { return 1; } int main() { bool b; b = std::is_same< typename function_traits<int(double)>::return_type, int>::value; std::cout << "is same: " << b << std::endl; b = std::is_same< typename function_traits<decltype(testfunc1)>::arg<0>::type, char>::value; std::cout << "is same: " << b << std::endl; }
对于各类参数类型的普通函数,都能正确推断。但重载函数的情形须要咱们考虑。以下咱们增长testfunc1的重载版本:指针
bool testfunc1(double, char) { return false; }
此时decltype(testfunc1)是没法编译经过的。这并非咱们的function_traits有问题。而是在没信息的状况下,decltype是没法选择testfunc1的重载版本的。除非咱们在function_traits显式特化。code
函数指针的function_traits也会遇到重载问题,以下是针对函数指针的function_traits:htm
template<typename R, typename... Args> struct function_traits<R (*)(Args...)> { typedef R return_type; typedef typelist<Args...> arglist; enum { arg_count = sizeof...(Args) }; template<unsigned int N> struct arg { typedef typename at<N, arglist>::type type; }; };
decltye(&testfunc1)也是没法编译经过的。很显然,你本身做为编译器做者的话,如果没有额外的信息,让你使用decltype去推断一个可重载的函数类型,你怎么可以知道用户但愿获得哪一个类型?除了显示特化以提供给编译器信息外,对于函数指针,咱们还能够提早转换,显式给以类型信息供编译器推断,以下:blog
int (*castfunc)(char) = &testfunc1;
b = std::is_same< typename function_traits<decltype(castfunc)>::arg<0>::type, char>::value; std::cout << "is same: " << b << std::endl;
castfunc1在定义时获得了testfunc1正确的重载类型,所以decltype在推断castfunc时就有了信息来选择正确的类型。get
这并非一个程序技术问题,更算是一个逻辑问题,就好像面对有多个定义的单词,没有上下文你是没法知道它要表明什么意思的。
这种显示转换并不会带给咱们太多困扰。由于使用function_traits的场景,基本上是一种延迟推断手段。好比获得消息后,使用泛型手法分发消息处理。而消息处理函数咱们在注册的时候确定是知道函数类型的,在注册时咱们就已经能够显示转换这个函数指针而不会遇到重载问题了。直接使用decltype(testfunc1)好像在咱们测试function_traits时才会遇到,嗯,另外一我的也遇到了,否则我不会试验。。。
然而确实存在一个可能,使咱们能够传入testfunc1,而不用给予完整类型信息,虽然不适用于function_traits的状况。以下:
template<typename ...Args> struct OverloadResolved { template<typename R> static auto static_doit( R (*f) (Args...), Args ... args ) -> R { return f(args...);} }; template<typename ...Args> auto deduce(Args...) -> OverloadResolved<Args...> { return OverloadResolved<Args...>(); } template<typename T> struct dummy : public T { }; #define doit(f, ...) ( dummy<decltype(deduce( __VA_ARGS__ ))> :: static_doit(f, __VA_ARGS__) )
使用:
char aa = 'a'; double ff = 0.1; std::cout << doit(testfunc1, aa) << " " << doit(testfunc1, ff, aa) << std::endl;
能够看到,虽然testfunc1有2个重载版本,但仍能正确的执行testfunc1(aa)和testfunc1(ff, aa).
固然由于此处给出了参数信息。这是一个运行时方案,而function_traits要求咱们在编译期就推断。
如下添加类成员函数的function_traits:
template <typename R, typename T, typename... Args> struct function_traits<R (T::*)(Args...)> { typedef T class_type; typedef R return_type; typedef typelist<Args...> arglist; enum { arg_count = sizeof...(Args) }; template<unsigned int N> struct arg { typedef typename at<N, arglist>::type type; }; };
还须要添加const,volatile修饰符的。如下是更完整的版本:
template<typename T> struct function_traits; template<typename R, typename... Args> struct function_traits<R (Args...)> { typedef R return_type; typedef typelist<Args...> arglist; enum { arg_count = sizeof...(Args) }; template<unsigned int N> struct arg { typedef typename at<N, arglist>::type type; }; }; template<typename R, typename... Args> struct function_traits<R (Args...) const> { typedef R return_type; typedef typelist<Args...> arglist; enum { arg_count = sizeof...(Args) }; template<unsigned int N> struct arg { typedef typename at<N, arglist>::type type; }; }; template<typename R, typename... Args> struct function_traits<R (Args...) volatile> { typedef R return_type; typedef typelist<Args...> arglist; enum { arg_count = sizeof...(Args) }; template<unsigned int N> struct arg { typedef typename at<N, arglist>::type type; }; }; template<typename R, typename... Args> struct function_traits<R (Args...) const volatile> { typedef R return_type; typedef typelist<Args...> arglist; enum { arg_count = sizeof...(Args) }; template<unsigned int N> struct arg { typedef typename at<N, arglist>::type type; }; }; template<typename R, typename... Args> struct function_traits<R (*)(Args...)> { typedef R return_type; typedef typelist<Args...> arglist; enum { arg_count = sizeof...(Args) }; template<unsigned int N> struct arg { typedef typename at<N, arglist>::type type; }; }; template <typename R, typename T, typename... Args> struct function_traits<R (T::*)(Args...)> { typedef T class_type; typedef R return_type; typedef typelist<Args...> arglist; enum { arg_count = sizeof...(Args) }; template<unsigned int N> struct arg { typedef typename at<N, arglist>::type type; }; }; template <typename R, typename T, typename... Args> struct function_traits<R (T::*)(Args...) const> { typedef T class_type; typedef R return_type; typedef typelist<Args...> arglist; enum { arg_count = sizeof...(Args) }; template<unsigned int N> struct arg { typedef typename at<N, arglist>::type type; }; }; template <typename R, typename T, typename... Args> struct function_traits<R (T::*)(Args...) volatile> { typedef T class_type; typedef R return_type; typedef typelist<Args...> arglist; enum { arg_count = sizeof...(Args) }; template<unsigned int N> struct arg { typedef typename at<N, arglist>::type type; }; }; template <typename R, typename T, typename... Args> struct function_traits<R (T::*)(Args...) const volatile> { typedef T class_type; typedef R return_type; typedef typelist<Args...> arglist; enum { arg_count = sizeof...(Args) }; template<unsigned int N> struct arg { typedef typename at<N, arglist>::type type; }; };