先说一个简单的方案. 经过验证 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. mac上安装chromedriver

    默认下载2.35版本,放置到/usr/local/bin #!/usr/bin/env bash # ------------------------------------------------- ...

  2. python -- DNS处理模块dnspython

    简介 dnspython – 是python实现的一个DNS工具包,利用其查询功能来实现dns的服务监控及解析结果的校验 安装dnspython pip install dnspython 使用 常见 ...

  3. Tomcat和JVM的性能调优总结

    Tomcat性能调优: 找到Tomcat根目录下的conf目录,修改server.xml文件的内容.对于这部分的调优,我所了解到的就是无非设置一下Tomcat服务器的最大并发数和Tomcat初始化时创 ...

  4. LFYZOJ 104 Counting Swaps

    题解 #include <iostream> #include <cstdio> #include <algorithm> #include <cmath&g ...

  5. Java语言编码规范 - Java语言编码规范(中文版)(http://doc.javanb.com/code-conventions-for-the-java-programming-language-zh/index.html)

      目录 1 介绍 1.1 为什么要有编码规范 1.2 版权声明 2 文件名 2.1 文件后缀 2.2 常用文件名 3 文件组织 3.1 Java源文件 3.1.1 开头注释 3.1.2 包和引入语句 ...

  6. uniSWF使用注意事项

    美术方面,也就是使用FLASH这里的用户,请注意以下几点, 1.把相同的图案做成元件: 2.凡是补间产生的动画物体,也要做成元件: 以上便可以节省大量的空间,因为当迩把图案做成元件的话,UNISWF导 ...

  7. LeetCode OJ——Two Sum

    http://oj.leetcode.com/problems/two-sum/ 求是否存在两个数的和为target,暴力法,两层循环 #include <iostream> #inclu ...

  8. AC日记——Weird Rounding Codeforces 779b

    B. Weird Rounding time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  9. vue生命周期回调方法

    最近在用vue开发一个商品列表页,因需要根据请求回的字段是否有内容来显示隐藏该字段, 但因为vue异步加载导致显示隐藏方法不起作业(主要是判断条件取不到页面渲染内容),围观了vue生命周期后发现upd ...

  10. PythonWeb开发教程(二),搭建第一个django项目

    这篇写怎么创建django项目,以及把django项目运行起来. 1.创建django项目 a.使用命令创建,安装完django之后就有django-admin命令了,执行命令创建即可,命令如下:   ...