先说一个简单的方案. 经过验证 g++ 和 vs2010 都可以.
原理就是利用函数类型可以直接转换成函数指针.

  1. template<class T> bool test( T * t )
  2. {
  3. return true;
  4. }
  5. bool test( ... )
  6. {
  7. return false;
  8. }
  9. #include<iostream>
  10. using namespace std;
  11. int main()
  12. {
  13. int k = 12;
  14. cout << test(main) << endl;
  15. cout << test( k ) << endl;
  16. typedef int(*PtrFun)();
  17. PtrFun ptr = main;
  18. cout << test(ptr) << endl;
  19. cout << test( 12 ) << endl;
  20. return 0;
  21. }

再说一个稍微复杂点的方案. g++ 编译没问题. vs2010 编译不过去.
利用了函数类型没有数组的概念. 入梅不存在某个类型的数组, 0 转换 U (*)[1] 自然会失败

  1. template<class T>
  2. class IsFunction
  3. {
  4. private:
  5. typedef char ONE;
  6. typedef struct{char a[2];} TWO;
  7. template<class U>static ONE test(...);
  8. template<class U>static TWO test(U (*)[1]);
  9. public:
  10. enum{YES = sizeof(IsFunction<T>::test<T>(0)) == 1 };
  11. enum{NO = !YES};
  12. };
  13. template<>
  14. class IsFunction<T&>
  15. {
  16. public:
  17. enum{YES = 0 };
  18. enum{NO = !YES};
  19. }
  20. template<>
  21. class IsFunction<void>
  22. {
  23. public:
  24. enum{YES = 0 };
  25. enum{NO = !YES};
  26. }
  27. template<>
  28. class IsFunction<const void>
  29. {
  30. public:
  31. enum{YES = 0 };
  32. enum{NO = !YES};
  33. }
  34. template<class T>
  35. long kkk(T&){return IsFunction<T>::YES;}
  36. #include<iostream>
  37. using namespace std;
  38. int main()
  39. {
  40. int k = 23;
  41. cout << kkk(main) << endl;
  42. cout << kkk(k) << endl;
  43. cout << IsFunction<int>::YES << endl;
  44. typedef int(*PtrFun)();
  45. PtrFun ptr = main;
  46. cout << IsFunction<PtrFun>::YES << endl;
  47. return 0;
  48. }

C++代码书写模板 -- 如何判断函数类型的更多相关文章

  1. golang中函数类型

    今天看Martini文档,其功能列表提到完全兼容http.HandlerFunc接口,就去查阅了Go: net/http的文档,看到type HandlerFunc这部分,顿时蒙圈了.由于之前学习的时 ...

  2. php 读取文件头判断文件类型的实现代码

    php代码实现读取文件头判断文件类型,支持图片.rar.exe等后缀. 例子: <?php $filename = "11.jpg"; //为图片的路径可以用d:/uploa ...

  3. PHP取二进制文件头快速判断文件类型的实现代码

    通过读取文件头信息来识别文件的真实类型. 一般我们都是按照文件扩展名来判断文件类型,但是这个很不靠谱,轻易就通过修改扩展名来躲避了,一般必须要读取文件信息来识别,PHP扩展中提供了类似 exif_im ...

  4. JS的数据类型判断函数、数组对象结构处理、日期转换函数,浏览器类型判断函数合集

    工具地址:https://github.com/BothEyes1993/bes-jstools bes-jstools 100多个基础常用JS函数和各种数据转换处理集合大全,此工具包是在 outil ...

  5. c++派生类中构造函数和析构函数执行顺序、判断对象类型、抽象类、虚函数

    一. 代码: 1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 #include&l ...

  6. PHP常用类型判断函数

    1.gettype():获取变量类型 2.is_array():判断变量类型是否为数组类型 3.is_double():判断变量类型是否为倍浮点类型 4.is_float():判断变量类型是否为浮点类 ...

  7. C#中判断bool 类型 代码的最短写法

    看到一个关于写最短代码的,  是一个bool类型判断的:    public bool IsNull(object val) { if (val == null) { return true; } e ...

  8. YUI的类型判断函数

    1.首先定义一个关于类型的对象,及相关变量 类型判断对象 ar L = Y.Lang || (Y.Lang = {}), STRING_PROTO = String.prototype, TOSTRI ...

  9. PHP常用类型判断函数总结

    1.gettype():获取变量类型 2.is_array():判断变量类型是否为数组类型 3.is_double():判断变量类型是否为倍浮点类型 4.is_float():判断变量类型是否为浮点类 ...

随机推荐

  1. 洛谷 P 1133 教主的花园

    题目描述 教主有着一个环形的花园,他想在花园周围均匀地种上n棵树,但是教主花园的土壤很特别,每个位置适合种的树都不一样,一些树可能会因为不适合这个位置的土壤而损失观赏价值. 教主最喜欢3种树,这3种树 ...

  2. 【kindeditor】KindEditor获取多个textarea文本框的值并判断非空

    kindeditor官网:http://kindeditor.net/demo.php 如何获取多个KindEditor中textarea文本框的值,方式很多种(带有HTML标签). var intr ...

  3. 视音频数据处理入门:RGB、YUV像素数据处理【转】

    转自:http://blog.csdn.net/leixiaohua1020/article/details/50534150 ==================================== ...

  4. linux内核栈与用户栈【转】

    转自:http://19880512.blog.51cto.com/936364/274610 最近linux内核的中断部分,总是被书里的栈弄晕,一会儿内核栈,一会儿用户栈的……很是崩溃,在网上goo ...

  5. 在centos7环境下建立MariaDB多实例

    环境全部基于vmware player 12 os: centos7 mariadb: mariadb-10.1.12-linux-x86_64.tar.gz 主要根据MariaDB给出的帮助文档,以 ...

  6. HOJ - 2715最小费用流

    国庆八天乐,刷题也快乐. HOJ崩了,但是VJ可以把题目挂出来. 题目链接:https://vjudge.net/contest/188441#problem/A 涉及到矩阵里面的网络流,化为图来做. ...

  7. python笔记4:高级特性

    4 高级特性 4.1  切片 [:] *注:-- list和tuple,字符串都支持切片 4.2 迭代 Iteration for ... in 及 for ... in if 两个变量迭代时, 例1 ...

  8. poj 2104 K-th Number(主席树

    Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 59058   Accepted: 20529 Case Time Limi ...

  9. PyTorch学习笔记之n-gram模型实现

    import torch import torch.nn as nn from torch.autograd import Variable import torch.nn.functional as ...

  10. Spring MVC中@ControllerAdvice注解实现全局异常拦截

    在网上很多都把Advice翻译成增强器,其实从翻译工具上看到,这个单词翻译是忠告,通知的意思. 首先这个注解实在Spring Web包下,而Spring MVC离不开Spring Web的依赖,所以经 ...