Linux C/C++ 模板:针对类型信息的特化
2015-04-11 00:51
211 查看
一、代码
#include <iostream>
using namespace std;
//针对类型信息的特化
//
template <typename T>
class IsInt
{
public:
enum { result = false };
};
//
template <>
class IsInt<int>
{
public:
enum { result = true };
};
int main(int argc, char*argv[])
{
IsInt<int>::result == true ? cout<<"int is int"<<endl : cout<<"int is not int"<<endl;
IsInt<char*>::result == true ? cout<<"char* is int"<<endl : cout<<"char* is not int"<<endl;
IsInt<float>::result == true ? cout<<"float is int"<<endl : cout<<"float is not int"<<endl;
return 0;
}
二、输出结果
#include <iostream>
using namespace std;
//针对类型信息的特化
//
template <typename T>
class IsInt
{
public:
enum { result = false };
};
//
template <>
class IsInt<int>
{
public:
enum { result = true };
};
int main(int argc, char*argv[])
{
IsInt<int>::result == true ? cout<<"int is int"<<endl : cout<<"int is not int"<<endl;
IsInt<char*>::result == true ? cout<<"char* is int"<<endl : cout<<"char* is not int"<<endl;
IsInt<float>::result == true ? cout<<"float is int"<<endl : cout<<"float is not int"<<endl;
return 0;
}
二、输出结果
相关文章推荐
- 针对类型信息的特化
- C++类模板的三种特化类型
- 非类型模板参数(针对C++)
- C++类模板的三种特化类型
- C++类模板的三种特化类型
- 通过模板的特化实现 简单的类型萃取 实现memcppy时候对于特殊类型如string类的拷贝。
- C++模板中type_traits(类型特化)
- 通过模板的特化实现 简单的类型萃取 实现memcppy时候对于特殊类型如string类的拷贝。
- 再论c++模板之类型识别之如何得到类型信息
- 用于datagrid模板针对不同的数据类型的参数归总
- Linux C/C++ 模板:嵌入的类型信息
- PowerShell针对SCVMM批量导出模板的描述信息以及导入
- 确定类型和表达式在编译期信息的模板技术
- [翻译] Effective C++, 3rd Edition, Item 46: 需要 type conversions(类型转换)时在 templates(模板)内定义 non-member functions(非成员函数)
- 用类型模板判断"数据"类型
- (转)列出SQL SERVER 所有表、字段定义,类型,长度,一个值等信息(另一种方法)
- 获取一个表的,字段,类型,长度,是否主键,是否为空,注释 等信息
- [翻译] Effective C++, 3rd Edition, Item 47: 为类型信息使用 traits classes(特征类)(下)
- 如何得到运行程序的路径,以及如何得到路径的文件夹,文件名,以及类型等等信息
- [翻译] Effective C++, 3rd Edition, Item 45: 用 member function templates(成员函数模板) 接受 "all compatible types"(“所有兼容类型”)