C++代码书写模板 -- 如何判断函数类型
先说一个简单的方案. 经过验证 g++ 和 vs2010 都可以.
原理就是利用函数类型可以直接转换成函数指针.
- template<class T> bool test( T * t )
- {
- return true;
- }
- bool test( ... )
- {
- return false;
- }
- #include<iostream>
- using namespace std;
- int main()
- {
- int k = 12;
- cout << test(main) << endl;
- cout << test( k ) << endl;
- typedef int(*PtrFun)();
- PtrFun ptr = main;
- cout << test(ptr) << endl;
- cout << test( 12 ) << endl;
- return 0;
- }
再说一个稍微复杂点的方案. g++ 编译没问题. vs2010 编译不过去.
利用了函数类型没有数组的概念. 入梅不存在某个类型的数组, 0 转换 U (*)[1] 自然会失败
- template<class T>
- class IsFunction
- {
- private:
- typedef char ONE;
- typedef struct{char a[2];} TWO;
- template<class U>static ONE test(...);
- template<class U>static TWO test(U (*)[1]);
- public:
- enum{YES = sizeof(IsFunction<T>::test<T>(0)) == 1 };
- enum{NO = !YES};
- };
- template<>
- class IsFunction<T&>
- {
- public:
- enum{YES = 0 };
- enum{NO = !YES};
- }
- template<>
- class IsFunction<void>
- {
- public:
- enum{YES = 0 };
- enum{NO = !YES};
- }
- template<>
- class IsFunction<const void>
- {
- public:
- enum{YES = 0 };
- enum{NO = !YES};
- }
- template<class T>
- long kkk(T&){return IsFunction<T>::YES;}
- #include<iostream>
- using namespace std;
- int main()
- {
- int k = 23;
- cout << kkk(main) << endl;
- cout << kkk(k) << endl;
- cout << IsFunction<int>::YES << endl;
- typedef int(*PtrFun)();
- PtrFun ptr = main;
- cout << IsFunction<PtrFun>::YES << endl;
- return 0;
- }
C++代码书写模板 -- 如何判断函数类型的更多相关文章
- golang中函数类型
今天看Martini文档,其功能列表提到完全兼容http.HandlerFunc接口,就去查阅了Go: net/http的文档,看到type HandlerFunc这部分,顿时蒙圈了.由于之前学习的时 ...
- php 读取文件头判断文件类型的实现代码
php代码实现读取文件头判断文件类型,支持图片.rar.exe等后缀. 例子: <?php $filename = "11.jpg"; //为图片的路径可以用d:/uploa ...
- PHP取二进制文件头快速判断文件类型的实现代码
通过读取文件头信息来识别文件的真实类型. 一般我们都是按照文件扩展名来判断文件类型,但是这个很不靠谱,轻易就通过修改扩展名来躲避了,一般必须要读取文件信息来识别,PHP扩展中提供了类似 exif_im ...
- JS的数据类型判断函数、数组对象结构处理、日期转换函数,浏览器类型判断函数合集
工具地址:https://github.com/BothEyes1993/bes-jstools bes-jstools 100多个基础常用JS函数和各种数据转换处理集合大全,此工具包是在 outil ...
- c++派生类中构造函数和析构函数执行顺序、判断对象类型、抽象类、虚函数
一. 代码: 1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 #include&l ...
- PHP常用类型判断函数
1.gettype():获取变量类型 2.is_array():判断变量类型是否为数组类型 3.is_double():判断变量类型是否为倍浮点类型 4.is_float():判断变量类型是否为浮点类 ...
- C#中判断bool 类型 代码的最短写法
看到一个关于写最短代码的, 是一个bool类型判断的: public bool IsNull(object val) { if (val == null) { return true; } e ...
- YUI的类型判断函数
1.首先定义一个关于类型的对象,及相关变量 类型判断对象 ar L = Y.Lang || (Y.Lang = {}), STRING_PROTO = String.prototype, TOSTRI ...
- PHP常用类型判断函数总结
1.gettype():获取变量类型 2.is_array():判断变量类型是否为数组类型 3.is_double():判断变量类型是否为倍浮点类型 4.is_float():判断变量类型是否为浮点类 ...
随机推荐
- 洛谷 P 1133 教主的花园
题目描述 教主有着一个环形的花园,他想在花园周围均匀地种上n棵树,但是教主花园的土壤很特别,每个位置适合种的树都不一样,一些树可能会因为不适合这个位置的土壤而损失观赏价值. 教主最喜欢3种树,这3种树 ...
- 【kindeditor】KindEditor获取多个textarea文本框的值并判断非空
kindeditor官网:http://kindeditor.net/demo.php 如何获取多个KindEditor中textarea文本框的值,方式很多种(带有HTML标签). var intr ...
- 视音频数据处理入门:RGB、YUV像素数据处理【转】
转自:http://blog.csdn.net/leixiaohua1020/article/details/50534150 ==================================== ...
- linux内核栈与用户栈【转】
转自:http://19880512.blog.51cto.com/936364/274610 最近linux内核的中断部分,总是被书里的栈弄晕,一会儿内核栈,一会儿用户栈的……很是崩溃,在网上goo ...
- 在centos7环境下建立MariaDB多实例
环境全部基于vmware player 12 os: centos7 mariadb: mariadb-10.1.12-linux-x86_64.tar.gz 主要根据MariaDB给出的帮助文档,以 ...
- HOJ - 2715最小费用流
国庆八天乐,刷题也快乐. HOJ崩了,但是VJ可以把题目挂出来. 题目链接:https://vjudge.net/contest/188441#problem/A 涉及到矩阵里面的网络流,化为图来做. ...
- python笔记4:高级特性
4 高级特性 4.1 切片 [:] *注:-- list和tuple,字符串都支持切片 4.2 迭代 Iteration for ... in 及 for ... in if 两个变量迭代时, 例1 ...
- poj 2104 K-th Number(主席树
Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 59058 Accepted: 20529 Case Time Limi ...
- PyTorch学习笔记之n-gram模型实现
import torch import torch.nn as nn from torch.autograd import Variable import torch.nn.functional as ...
- Spring MVC中@ControllerAdvice注解实现全局异常拦截
在网上很多都把Advice翻译成增强器,其实从翻译工具上看到,这个单词翻译是忠告,通知的意思. 首先这个注解实在Spring Web包下,而Spring MVC离不开Spring Web的依赖,所以经 ...