先说一个简单的方案. 经过验证 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. linux监控平台搭建-cpu

    linux监控平台搭建-cpu 目前服务器的主流CPU是intel或者AMD.到底主频是什么.多核.多线程.并发.并行.超频.一级缓存.二级缓存.三级缓存.i386.x86 cpu:含有算术逻辑.控制 ...

  2. Linux System Programming 学习笔记(三) 标准缓冲I/O

    1. partial block operations are inefficient. The operating system has to “fix up” your I/O by ensuri ...

  3. Linux Malloc分析-从用户空间到内核空间【转】

    转自:http://blog.csdn.net/ordeder/article/details/41654509 版权声明:本文为博主(http://blog.csdn.net/ordeder)原创文 ...

  4. 什麼是 struct,union,enumeration 的 tag ?

    struct tag { member-list }; union tag { member-list }; enum tag { member-list }; union test1 { int a ...

  5. ajax操作提交整个表单内容

    1 2 3 4 5 6 7 8 9 10 11 12 13 $.ajax({                 cache: true,                 type: "POST ...

  6. JS快速上手-基础Javascript

    1.1背景 1.1.1 ECMAScript与javascript ECMAScript是javascript的官方命名.因为java已经是一个商标.如今,一些早前收到过授权的公司,如Moailla, ...

  7. python 终端模拟模块 pexpect

    简单介绍pexpect是 Don Libes 的 Expect 语言的一个 Python 实现,是一个用来启动子程序,并使用正则表达式对程序输出做出特定响应,以此实现与其自动交互的 Python 模块 ...

  8. 多核服务器的JVM优化选项(转载)

    原文链接 现在多核CPU是主流.利用多核技术,可以有效发挥硬件的能力,提升吞吐量,对于Java程序,可以实现并发垃圾收集.但是Java利用多核技术也带来了一些问题,主要是多线程共享内存引起了.目前内存 ...

  9. Ant -----ant标签和自定义任务

    随便记一下 Ant的用法吧.ant ,maven, gradle ,三个打包工具到齐了,Ant 常见标签解析,ant 自定义task . <?xml version="1.0" ...

  10. Maven创建Java Application工程(既jar包)

    Maven在创建工程时使用的是archetype(原型)插件,而如果要创建具体的工程,比如Application这些,那么可以使用maven-archetype-quickstart(相当于一个子类型 ...