function traits.

获取函数或成员函数的返回类型,参数类型,参数长度,类类型。

函数参数列表推断基于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;
};
};

使用:

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显式特化。

函数指针的function_traits也会遇到重载问题,如下是针对函数指针的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;
};
};

decltye(&testfunc1)也是无法编译通过的。很显然,你自己作为编译器作者的话,若是没有额外的信息,让你使用decltype去推断一个可重载的函数类型,你怎么能够知道用户希望得到哪个类型?除了显示特化以提供给编译器信息外,对于函数指针,我们还可以提前转换,显式给以类型信息供编译器推断,如下:

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时就有了信息来选择正确的类型。

这并不是一个程序技术问题,更算是一个逻辑问题,就好像面对有多个定义的单词,没有上下文你是无法知道它要代表什么意思的。

这种显示转换并不会带给我们太多困扰。因为使用function_traits的场景,基本上是一种延迟推断手段。比如得到消息后,使用泛型手法分发消息处理。而消息处理函数我们在注册的时候肯定是知道函数类型的,在注册时我们就已经可以显示转换这个函数指针而不会遇到重载问题了。直接使用decltype(testfunc1)好像在我们测试function_traits时才会遇到,嗯,另一个人也遇到了,不然我不会试验。。。

然而确实存在一个可能,使我们可以传入testfunc1,而不用给予完整类型信息,虽然不适用于function_traits的情况。如下:

http://stackoverflow.com/questions/9054703/overloaded-function-as-argument-of-variadic-template-function

    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;
};
};

c++11 function_typetraits备忘的更多相关文章

  1. opencv-2.4.11编译备忘

    编译完成后,想测试example中例子,但是由于没有sudo权限,不能运行pkg-config查看opencv的--cflags和--libs. 记录一下,备忘: pkg-config --libs ...

  2. 11. 星际争霸之php设计模式--备忘模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  3. Nmap备忘单:从探索到漏洞利用(Part 2)

    这是我们的第二期NMAP备忘单(第一期在此).基本上,我们将讨论一些高级NMAP扫描的技术,我们将进行一个中间人攻击(MITM).现在,游戏开始了. TCP SYN扫描 SYN扫描是默认的且最流行的扫 ...

  4. [原]TCP/UDP使用细节备忘

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

  5. HTML5终极备忘大全

    二.文字备忘之标签 HTML5中新增的标签 <article> 定义文章 <aside> 定义页面内容旁边的内容 <audio> 定义声音内容 <canvas ...

  6. [转] HTML5终极备忘大全(图片版+文字版)---张鑫旭

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=1544 一.前言兼图片 ...

  7. Linq to XML 读取XML 备忘笔记

    本文转载:http://www.cnblogs.com/infozero/archive/2010/07/13/1776383.html Linq to XML 读取XML 备忘笔记 最近一个项目中有 ...

  8. Python中利用函数装饰器实现备忘功能

    Python中利用函数装饰器实现备忘功能 这篇文章主要介绍了Python中利用函数装饰器实现备忘功能,同时还降到了利用装饰器来检查函数的递归.确保参数传递的正确,需要的朋友可以参考下   " ...

  9. 基于Prism.Windows的UWP开发备忘

    以前做UWP开发都是使用MvvmLight,主要是简单易上手,同时也写了很多MvvmLight的开发系列文章: UWP开发必备以及常用知识点总结 UWP开发之Mvvmlight实践九:基于MVVM的项 ...

随机推荐

  1. Node.js简述

    Node.js是2009年5月由Ryan Dahl 发布的服务器程序. 它封装了Google V8 JavaScript 引擎, 并将其重建为可在服务器上使用. 它旨在提供一种简单的构建可伸缩网络程序 ...

  2. gii的使用

    假设有表link 在backend中生成子模块content 生成子模块模型common/models/Link 生成模块content中的crud 配置别名 在backend/config/main ...

  3. 如何从零开始在github上新建项目

    准备工作: (1)安装git: Git-2.16.1-64-bit.exe (2)新建一个文件夹grpc007,作为本地git仓库 (3)进入到grpc007目录,右键/打开git bash.使用gi ...

  4. ireport使用

    首先需要下载ireport,可到https://zh.osdn.net/projects/sfnet_ireport/releases/下载,可能打开速度有点慢,耐心等待下,里面有各个版本,可自行选择 ...

  5. Android代码安全工具集

    前言 原计划出一系列APP测试文章,从基础发,整个思路还在整理,秉着吹牛的态度,整理了一部分安卓代码安全的工具推荐给大家玩玩,提升一下逼格. 在这之前给大家讲讲阿旺对安全测试的理解,不管别人怎么扯,一 ...

  6. python全栈开发笔记---------数据类型****整理****

    一.数字 int(..) 二.字符串 replace/find/join/strip/startswith/split/upper/lower/format tempalet ='i am {name ...

  7. jmeter操作练习

    1.登录: 2.用户定义变量 3.http请求默认值 这是登录里面的服务器名称或IP可以不用填(以上情况是当多个共用一个IP的时候,可以这样做) 4.配置文件下--CSV Data Set Confi ...

  8. python自学第9天,装饰器

    装饰器:本质是函数(装饰其它函数) 就是为其它函数添加附加功能 原则:1.不能修改被装饰函数的源代码 2.不能修改被装饰的函数的调用方式 实现装饰器知识储备: 1.函数即变量 2.高阶函数:a.把一个 ...

  9. split根据一个元素分割语句

    a = ‘https://www.baidu.com/s?wd=%E7%AE%80%E4%B9%A6&rsv_spt=1&rsv_iqid=0xdfa3666f00083a6f& ...

  10. centos7.3 64位 安装git

    1.安装编译git时需要的包 # yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel # yum ins ...