C++11 type_traits 之is_pointer,is_member_function_pointer源码分析
源码如下:
template<typename>
struct __is_pointer_helper
: public false_type { }; template<typename _Tp>
struct __is_pointer_helper<_Tp*>
: public true_type { }; /// is_pointer
template<typename _Tp>
struct is_pointer
: public integral_constant<bool, (__is_pointer_helper<typename
remove_cv<_Tp>::type>::value)>
{ };
首先,定义了两个类型,一个true_type和一个false_type这两个值均继承integral_constant。这两个类型几乎被所有的is_xxx复用啦。而且标准库也提供给我们使用。
然后,模板偏特化,指针类型的版本继承true_type,非指针类型的版本继承了false_type。
/// integral_constant
template<typename _Tp, _Tp __v>
struct integral_constant
{
static constexpr _Tp value = __v;
typedef _Tp value_type;
typedef integral_constant<_Tp, __v> type;
constexpr operator value_type() { return value; }
}; template<typename _Tp, _Tp __v>
constexpr _Tp integral_constant<_Tp, __v>::value; /// The type used as a compile-time boolean with true value.
typedef integral_constant<bool, true> true_type; /// The type used as a compile-time boolean with false value.
typedef integral_constant<bool, false> false_type;
template<typename>
struct __is_member_function_pointer_helper
: public false_type { }; template<typename _Tp, typename _Cp>
struct __is_member_function_pointer_helper<_Tp _Cp::*>
: public integral_constant<bool, is_function<_Tp>::value> { }; /// is_member_function_pointer
template<typename _Tp>
struct is_member_function_pointer
: public integral_constant<bool, (__is_member_function_pointer_helper<
typename remove_cv<_Tp>::type>::value)>
{ };
成员指针,稍微复杂一点,和一般指针类似,成员指针的偏特化要写成这样_Tp _Cp::*。
// Primary template.
/// Define a member typedef @c type to one of two argument types.
template<bool _Cond, typename _Iftrue, typename _Iffalse>
struct conditional
{ typedef _Iftrue type; }; // Partial specialization for false.
template<typename _Iftrue, typename _Iffalse>
struct conditional<false, _Iftrue, _Iffalse>
{ typedef _Iffalse type; };
conditional机制类似于loki中的Select,根据boolean值来选择类型,如果_Cond为true,则选择_Iftrue类型,否则选择另一个。
/// is_reference
template<typename _Tp>
struct is_reference
: public __or_<is_lvalue_reference<_Tp>,
is_rvalue_reference<_Tp>>::type
{ };
is_reference通过or结合左值引用和右值引用判断。
template<typename...>
struct __or_; template<>
struct __or_<>
: public false_type
{ }; template<typename _B1>
struct __or_<_B1>
: public _B1
{ }; template<typename _B1, typename _B2>
struct __or_<_B1, _B2>
: public conditional<_B1::value, _B1, _B2>::type
{ }; template<typename _B1, typename _B2, typename _B3, typename... _Bn>
struct __or_<_B1, _B2, _B3, _Bn...>
: public conditional<_B1::value, _B1, __or_<_B2, _B3, _Bn...>>::type
{ };
1.or继承conditional所提供的类型而不是conditional本身。
2.conditional通过or中第一类型boolean来提供不同的类型,如果B1的boolean是true则继承(也表明B1继承了true_type),不是则继承剩余参数or(递归继承下去)。
3.递归下去,就剩下一个类型时,则直接继承B1,B1的boolean就是整个or的结果
4.递归到末尾空参数,继承false_type,整个or的结果即为false。
C++11 type_traits 之is_pointer,is_member_function_pointer源码分析的更多相关文章
- 多线程高并发编程(11) -- 非阻塞队列ConcurrentLinkedQueue源码分析
一.背景 要实现对队列的安全访问,有两种方式:阻塞算法和非阻塞算法.阻塞算法的实现是使用一把锁(出队和入队同一把锁ArrayBlockingQueue)和两把锁(出队和入队各一把锁LinkedBloc ...
- jQuery1.6源码分析系列
原文地址:http://www.cnblogs.com/nuysoft/archive/2011/11/14/2248023.html jQuery源码分析(版本1.6.1) 目录 00 前言开光 0 ...
- YYCache 源码分析(一)
iOS 开发中总会用到各种缓存,YYCache或许是你最好的选择.性能上有优势,用法也很简单.作者ibireme曾经对比过同类轮子:http://blog.ibireme.com/2015/10/26 ...
- Java 集合系列11之 Hashtable详细介绍(源码解析)和使用示例
概要 前一章,我们学习了HashMap.这一章,我们对Hashtable进行学习.我们先对Hashtable有个整体认识,然后再学习它的源码,最后再通过实例来学会使用Hashtable.第1部分 Ha ...
- Solr4.8.0源码分析(11)之Lucene的索引文件(4)
Solr4.8.0源码分析(11)之Lucene的索引文件(4) 1. .dvd和.dvm文件 .dvm是存放了DocValue域的元数据,比如DocValue偏移量. .dvd则存放了DocValu ...
- 比特币源码分析--C++11和boost库的应用
比特币源码分析--C++11和boost库的应用 我们先停下探索比特币源码的步伐,来分析一下C++11和boost库在比特币源码中的应用.比特币是一个纯C++编写的项目,用到了C++11和bo ...
- string源码分析 ——转载 http://blogs.360.cn/360cloud/2012/11/26/linux-gcc-stl-string-in-depth/
1. 问题提出 最近在我们的项目当中,出现了两次与使用string相关的问题. 1.1. 问题1:新代码引入的Bug 前一段时间有一个老项目来一个新需求,我们新增了一些代码逻辑来处理这个新需求.测试阶 ...
- C# DateTime的11种构造函数 [Abp 源码分析]十五、自动审计记录 .Net 登陆的时候添加验证码 使用Topshelf开发Windows服务、记录日志 日常杂记——C#验证码 c#_生成图片式验证码 C# 利用SharpZipLib生成压缩包 Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库
C# DateTime的11种构造函数 别的也不多说没直接贴代码 using System; using System.Collections.Generic; using System.Glob ...
- 【转】Java 集合系列11之 Hashtable详细介绍(源码解析)和使用示例
概要 前一章,我们学习了HashMap.这一章,我们对Hashtable进行学习.我们先对Hashtable有个整体认识,然后再学习它的源码,最后再通过实例来学会使用Hashtable.第1部分 Ha ...
随机推荐
- 增加oracle表空间
查找用户对应的表空间 1.查询表空间物理文件路径select tablespace_name, file_id, file_name,round(bytes/(1024*1024),0) total_ ...
- oracle入门(一)
### 一.体系结构 1. 数据库 : 只有一个数据库 2. 实例 : 后台运行的一个进程 3. 表空间: 逻辑存储单位 4. 数据文件: 物理存储单位 5. 用户:面向用户管理,由用户来管理表空间, ...
- 【js】中的小技巧
本文主要介绍一些JS中用到的小技巧 1. 类型强制转换 1.1 string强制转换为数字 可以用*1来转化为数字(实际上是调用.valueOf方法) 然后使用Number.isNaN来判断是否为 ...
- Resharp常用设置收集整理
F12跳转的问题:
- ORA-12541:TNS:无监听程序问题
这种情况可能有多种原因,解决办法如下: 方法1.原因:监听日志listener.log过大,超过4. 步骤: a.暂停监听服务 b.删除listener.log,文件位置:E:\app\Adminis ...
- ethereum(以太坊)(实例)--"安全的远程购买"
pragma solidity ^0.4.10; contract Safebuy{ uint public price; address public seller; address public ...
- TCC : Tiny C Compiler (2018-2-6)
饭墙下载,有缘上传: https://files.cnblogs.com/files/bhfdz/tcc-0.9.27-win32-bin.zip https://files.cnblogs.com/ ...
- Oracle之单表查询及常用函数
1.语法: select 字段列表 from 表名 [where 查询条件] [group by 分组] [having 分组条件] [order by 排序] select * 代表查询所有的字段 ...
- Linux字符设备学习,总结
注册字符驱动的一种老方法: 注册一个字符设备的经典方法是使用:int register_chrdev(unsigned int major, const char *name, structfile_ ...
- STM32学习日志
今天是开学第二周周末 写这篇博客纯属是为了记住一些学到的知识,大佬勿喷.. 首先学32要知道一些选型知识,32中常用的芯片类型(某宝常卖的开发板芯片大多是这几种):C8T6,RBT6,RCT6,VET ...