c++ template 判断是否为类类型
/* The following code example is taken from the book
* "C++ Templates - The Complete Guide"
* by David Vandevoorde and Nicolai M. Josuttis, Addison-Wesley, 2002
*
* (C) Copyright David Vandevoorde and Nicolai M. Josuttis 2002.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#include <iostream> template<typename T>
class IsClassT {
private:
typedef char One;
typedef struct { char a[2]; } Two;
template<typename C> static One test(int C::*);
template<typename C> static Two test(...);
public:
enum { Yes = sizeof(test<T>(0)) == 1 };
enum { No = !Yes };
}; class MyClass {
}; struct MyStruct {
}; union MyUnion {
}; void myfunc()
{
} enum E { e1 } e; // check by passing type as template argument
template <typename T>
void check()
{
if (IsClassT<T>::Yes) {
std::cout << " IsClassT " << std::endl;
}
else {
std::cout << " !IsClassT " << std::endl;
}
} // check by passing type as function call argument
template <typename T>
void checkT(T)
{
check<T>();
} int main()
{
std::cout << "int: ";
check<int>(); std::cout << "MyClass: ";
check<MyClass>(); std::cout << "MyStruct:";
MyStruct s;
checkT(s); std::cout << "MyUnion: ";
check<MyUnion>(); std::cout << "enum: ";
checkT(e); std::cout << "myfunc():";
checkT(myfunc);
}
http://blog.csdn.net/zhoudaxia/article/category/553198
c++ template 判断是否为类类型的更多相关文章
- js获取或判断任意数据类类型的通用方法(getDataType)和将NodeList转为数组(NodeListToArray)
function getDataType(any){ /* (1) Object.prototype.toString.call 方法判断类型: 优点:通用,返回"[object Strin ...
- SWIFT中用Switch case 类类型
有时觉得SWIFT的语法真的强大而又变态,不说了,直接上代码瞅瞅: 首先先定义一个交通工具的父类 class Vehicle{ var wheels:Int! var speed:Double! in ...
- 【Python】Python—判断变量的基本类型
type() >>> type(123)==type(456) True >>> type(123)==int True >>> type('ab ...
- OpenCv中基本数据类型--Point,Size,Rect,Scalar,Vec3b类类型的详细解释
头文件路径:opencv-2.4.9/modules/core/include/opencv2/core/core.hpp 一.Point类 在这些数据类型中,最简单的就是Point点类,Point类 ...
- 自定义数据类型 C++ 结构体类型 共同体类型 枚举类型 类类型{}
一.结构体类型 结构体类型,共用体类型,枚举类型,类类型等统称为自定义类型(user-defined-type,UDT). 结构体相当于其他高级语言中的记录(record);例如: struct St ...
- php 通过PATH_SEPARATOR判断当前服务器系统类型
PATH_SEPARATOR是php中的一个预定义常量,我们可以直接echo这个常量,在linux系统中,该常量输出":",在windows系统中,该常量输出";&quo ...
- C++primer读书笔记9-转换和类类型
有时指定自己的类类型来表示某些类型的数据,如SmallInt,然后在为了便于计算将指定一个转换算,类类型,在某些情况下,自己主动转换为指定的类型 <1>转换操作符 operator typ ...
- C++ Primer 学习笔记_63_重载运算符和转换 --转换和类类型【上】
重载运算符和转换 --转换与类类型[上] 引言: 在前面我们提到过:能够用一个实參调用的位 unsignedchar 相同范围的值,即:0到255. 这个类能够捕获下溢和上溢错误,因此使用起来比内置u ...
- JAVA判断文件的内容类型
Java 7 新的特性,判断文件的内容类型. Program to demonstrate Java 7 new feature : Determining the file content type ...
随机推荐
- 最佳C/C++编辑器 source insight3
C/C++嵌入式代码编辑器source insight3下载地址 http://www.sourceinsight.com/eval.html 注册码:SI3US-361500-17409
- 7-19 PAT Judge(25 分)
The ranklist of PAT is generated from the status list, which shows the scores of the submissions. Th ...
- 《DSP using MATLAB》示例 Example 9.10
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- 在VS2008中使用WSE 3.0【转】
原文:http://www.cnblogs.com/chenxizhang/archive/2008/07/25/1251626.html 在VS2008中使用WSE 3.0 WSE 是微软推出的一套 ...
- ACCESS_TOKEN与FRESH_TOKEN
OAuth1.0中的access_token过期时间通常很长,安全性差.于是OAuth2.0推出了refresh_token. OAuth2.0中,客户端用账户名,密码经过一定方式(比如先请求code ...
- 几个基于jvm 的微服务框架
一个简单的整理,留待深入学习 micronaut http://micronaut.io/ sparkjava http://saprkjava.com spring cloud http://pro ...
- Archiva与maven配置使用
在之前的博文里头已经介绍了Archiva私服的使用,本文主要介绍,如何与maven进行配置,在进行maven使用的时候可以自动上传至Archiva上 1.设置maven的用户配置,到maven的安装目 ...
- linux之 LVM扩容
1. 查看本机现在磁盘的情况[root@oralce10g ~]# df Filesystem 1K-blocks Used Available Use% Mounted on/dev/mapper/ ...
- CentOS7 RPM安装 rabbitmqDownloads on Bintray
下载 0依赖Erlang RPM for RabbitMQ包(https://github.com/rabbitmq/erlang-rpm) https://dl.bintray.com/rabbit ...
- 由spring的工厂构造bean想到的
被Spring管理的bean可以是直接创建实例,还可以通过工厂模式来进行创建.例如brave的tracing bean定义: <bean id="tracing" class ...