C++代码书写模板 -- 如何判断函数类型
2013-11-15 14:34
483 查看
先说一个简单的方案. 经过验证 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;
}
原理就是利用函数类型可以直接转换成函数指针.
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++][语言语法]函数模板和模板函数 及参数类型的运行时判断
- C++泛型编程技巧 - 如何判断输入类型是否定义了特定的成员函数
- eclipse develop for C\C++ 文件注释函数注释代码模板自动生成方法
- C++ - 成员函数(member function)模板(template) 详解 及 代码
- smarty模板配置代码详细说明及如何注册自己的smarty函数
- C++中使用模板传递函数类型
- 再论c++模板之类型识别之如何得到类型信息
- 使用php判断浏览器的类型和语言的函数代码
- C++ - 成员函数(member function)模板(template) 详解 及 代码
- c++ 模板学习笔记:函数模板的类型识别(权哥)
- 如何在编译时刻判断两个类型是否可以自动转换?——《Modern C++ Design》读书笔记(1)
- C++ - 非类型模板参数(nontype template parameters) 使用 及 代码
- dedecms模板标签如何做判断示例代码
- C语言简单宏函数实现C++模板功能(类型本身作为函数参数)
- 求变量的数据类型,typeid,bool,C和C++的不同,new和delete,C++中的枚举,inline和可变参数模板,auto和函数模板,宽字符
- C++ - 非类型模板参数(nontype template parameters) 使用 及 代码
- 用C++写个程序,如何判断一个操作系统是16位还是32位的?不能用sizeof()函数?(不用sizeof()函数求当前主机上的一个int占用几个字节)
- C++的运算符重载(运算符重载如何选定重载的函数类型)--思考1
- C++箴言:用成员函数模板接受兼容类型
- 函数调用,C# 调用Dll中非托管C++代码时,函数参数的类型对照