请看源码:

template<typename _Tp, _Tp __v>
struct integral_constant
{
static const _Tp value = __v;
typedef _Tp value_type;
typedef integral_constant<_Tp, __v> type;
}; /// typedef for true_type
typedef integral_constant<bool, true> true_type; /// typedef for false_type
typedef integral_constant<bool, false> false_type; template<typename, typename>
struct is_same
: public false_type { }; template<typename _Tp>
struct is_same<_Tp, _Tp>
: public true_type { };

1. is_same是模版,integral_constant也是模版

2. true_type和false_type是integral_constant实例化的类型

3.is_same通用的模版继承了false_type

4.is_same的偏特化模版继承了true_type

5.is_same模版实参类型一致时,会实例化偏特化模版,而偏特化模版继承了true_type

6.integral_constant<bool, true> 即truetype的value是true

is_same和static_assert配合使用,可以在编译期进行强大的类型检查。使用例子如下:

 struct A{~A(){cout<<"delete A..."<<endl;}};
template<typename T>
struct TypeTraits
{
typedef void TYPE;
};
template<>
struct TypeTraits<std::string>
{
typedef std::string TYPE;
};
template<>
struct TypeTraits<long>
{
typedef long TYPE;
};
template<>
struct TypeTraits<A>
{
typedef A TYPE;
};
template<>
struct TypeTraits<double>
{ typedef double TYPE;
}; class DeleteLong
{
public:
void operator()(void *p)
{
delete static_cast<long*>(p);
}
};
class DeleteString
{
public:
void operator()(void *p)
{
delete static_cast<string*>(p);
}
};
class DeleteDouble
{
public:
void operator()(void *p)
{
delete static_cast<double*>(p);
}
};
class DeleteA
{
public:
void operator()(void *p)
{
delete static_cast<A*>(p);
}
}; class ExportData
{
void* vp;
enum my_type {SP,LP,DP,AP} types;
static unordered_map<type_index,my_type> typeMap;
static vector<function<void(void*)>> deleters;
public: template <typename T> ExportData(const T& t)
{
static_assert(is_same<typename TypeTraits<T>::TYPE,T>::value,"not support!");
vp=new T(t);
types= typeMap[typeid(T)];
}
template <typename T> void setData(const T& t)
{
static_assert(is_same<typename TypeTraits<T>::TYPE,T>::value,"not support!");
assert(types==typeMap[typeid(T)]);
*(static_cast<T*>(vp))=t;
}
template <typename T> void getData(T& t)
{
static_assert(is_same<typename TypeTraits<T>::TYPE,T>::value,"not support!");
assert(types==typeMap[typeid(T)]);
t=*(static_cast<T*>(vp));
} ~ExportData()
{
(deleters[types])(vp);
} }; unordered_map<type_index,ExportData::my_type> ExportData::typeMap
{
{typeid(string),ExportData::my_type::SP},
{typeid(long),ExportData::my_type::LP},
{typeid(double),ExportData::my_type::DP},
{typeid(A),ExportData::my_type::AP}
};
vector<function<void(void*)>> ExportData::deleters {DeleteString(),DeleteLong(),DeleteDouble(),DeleteA()};

static_assert(is_same<typename TypeTraits<T>::TYPE,T>::value,"not support!");静态断言,当用户使用不支持类型时,立即阻止用户编译。

C++11 type_traits 之is_same源码分析的更多相关文章

  1. C++11 type_traits 之is_convertible源码分析

    请看源码: struct __sfinae_types { typedef char __one; typedef ]; } __two; }; template<typename _From, ...

  2. C# DateTime的11种构造函数 [Abp 源码分析]十五、自动审计记录 .Net 登陆的时候添加验证码 使用Topshelf开发Windows服务、记录日志 日常杂记——C#验证码 c#_生成图片式验证码 C# 利用SharpZipLib生成压缩包 Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库

    C# DateTime的11种构造函数   别的也不多说没直接贴代码 using System; using System.Collections.Generic; using System.Glob ...

  3. 【Zookeeper】源码分析目录

    Zookeeper源码分析目录如下 1. [Zookeeper]源码分析之序列化 2. [Zookeeper]源码分析之持久化(一)之FileTxnLog 3. [Zookeeper]源码分析之持久化 ...

  4. Solr4.8.0源码分析(11)之Lucene的索引文件(4)

    Solr4.8.0源码分析(11)之Lucene的索引文件(4) 1. .dvd和.dvm文件 .dvm是存放了DocValue域的元数据,比如DocValue偏移量. .dvd则存放了DocValu ...

  5. 比特币源码分析--C++11和boost库的应用

    比特币源码分析--C++11和boost库的应用     我们先停下探索比特币源码的步伐,来分析一下C++11和boost库在比特币源码中的应用.比特币是一个纯C++编写的项目,用到了C++11和bo ...

  6. string源码分析 ——转载 http://blogs.360.cn/360cloud/2012/11/26/linux-gcc-stl-string-in-depth/

    1. 问题提出 最近在我们的项目当中,出现了两次与使用string相关的问题. 1.1. 问题1:新代码引入的Bug 前一段时间有一个老项目来一个新需求,我们新增了一些代码逻辑来处理这个新需求.测试阶 ...

  7. python基础-11 socket,IO多路复用,select伪造多线程,select读写分离。socketserver源码分析

    Socket socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. sock ...

  8. 11.深入k8s:kubelet工作原理及源码分析

    转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com 源码版本是1.19 kubelet信息量是很大的,通过我这一篇文章肯定是讲不全的,大家可 ...

  9. JDK源码分析(11)之 BlockingQueue 相关

    本文将主要结合源码对 JDK 中的阻塞队列进行分析,并比较其各自的特点: 一.BlockingQueue 概述 说到阻塞队列想到的第一个应用场景可能就是生产者消费者模式了,如图所示: 根据上图所示,明 ...

随机推荐

  1. HDU 2031 进制转换(10进制转R进制)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2031 进制转换 Time Limit: 2000/1000 MS (Java/Others)    M ...

  2. EF Core 2.1 中的 Eager loading、Explicit loading和LazyLoading (转自MSDN)

    Entity Framework Core allows you to use the navigation properties in your model to load related enti ...

  3. elementUI之switch应用的坑

    前言: 因为项目中用到了饿了么出品的element-ui这一套ui框架,所以很多地方都踩在了坑里,前面碰到了一些,今天着重聊一下switch这个组件. 首先switch接受Boolean类型的数据,莫 ...

  4. Java之环境变量配置

    1.首先安装Java的JDK(Java开发工具包 包含JRE(Java运行环境))下载地址URL:www.oracle.com (64位或32位) 安装:傻瓜式安装(点击下一步即可)中间可更改安装目录 ...

  5. JavaScript中Array的正确使用方式

    在 JavaScript 中正确使用地使用 Array 的方法如下: 用 Array.includes 代替 Array.indexOf “如果你要在数组中查找元素,请使用 Array.indexOf ...

  6. hql返回数值

    public int getCountUser() throws ParseException { Session hSession = sessionFactory.getCurrentSessio ...

  7. Window10 Electron 开发环境搭建及打包exe程序

    1.安装 Electron 首先要安装Node.js     (安装方法:https://www.cnblogs.com/inkwhite/p/9685520.html) 我这里已经安装好了. 2:安 ...

  8. LinkedList的源码分析(基于jdk1.8)

    1.初始化 public LinkedList() { } 并未开辟任何类似于数组一样的存储空间,那么链表是如何存储元素的呢? 2.Node类型 存储到链表中的元素会被封装为一个Node类型的结点.并 ...

  9. 『Linux基础 - 2 』操作系统,Linux背景知识和Ubuntu操作系统安装

    这篇笔记记录了以下几个知识点: 1.目前常见的操作系统及分类,虚拟机 2.Linux操作系统背景知识,Windows和Linux两个操作系统的对比 3.在虚拟机中安装Ubuntu系统的详细步骤 OS( ...

  10. AngularJS-Learning ui-router angular-transitions

    https://github.com/mgechev/AngularJS-Learning https://github.com/angular-ui/ui-router https://github ...