源码如下:

  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源码分析的更多相关文章

  1. 多线程高并发编程(11) -- 非阻塞队列ConcurrentLinkedQueue源码分析

    一.背景 要实现对队列的安全访问,有两种方式:阻塞算法和非阻塞算法.阻塞算法的实现是使用一把锁(出队和入队同一把锁ArrayBlockingQueue)和两把锁(出队和入队各一把锁LinkedBloc ...

  2. jQuery1.6源码分析系列

    原文地址:http://www.cnblogs.com/nuysoft/archive/2011/11/14/2248023.html jQuery源码分析(版本1.6.1) 目录 00 前言开光 0 ...

  3. YYCache 源码分析(一)

    iOS 开发中总会用到各种缓存,YYCache或许是你最好的选择.性能上有优势,用法也很简单.作者ibireme曾经对比过同类轮子:http://blog.ibireme.com/2015/10/26 ...

  4. Java 集合系列11之 Hashtable详细介绍(源码解析)和使用示例

    概要 前一章,我们学习了HashMap.这一章,我们对Hashtable进行学习.我们先对Hashtable有个整体认识,然后再学习它的源码,最后再通过实例来学会使用Hashtable.第1部分 Ha ...

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

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

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

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

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

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

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

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

  9. 【转】Java 集合系列11之 Hashtable详细介绍(源码解析)和使用示例

    概要 前一章,我们学习了HashMap.这一章,我们对Hashtable进行学习.我们先对Hashtable有个整体认识,然后再学习它的源码,最后再通过实例来学会使用Hashtable.第1部分 Ha ...

随机推荐

  1. 屏蔽JS代码错误

    用来屏蔽IE的错误的JS代码,也能屏蔽弹出框错误! <SCRIPT language=JavaScript> function killErrors() { return true; } ...

  2. 史上最简单的SpringCloud教程 | 第十篇: 高可用的服务注册中心(Finchley版本)

    转载请标明出处: 原文首发于 https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f10-eureka/ 本文出自方志朋的博客 文章 史上最简单 ...

  3. 基于java的简易计算器实现

    方法: 1.将string类型的表达式输入转换成后缀表达式 2.计算后缀表达式 步骤一:将string类型的表达式输入转换成后缀表达式 输入字符串表达式,并将表达式转换成char型数组 String ...

  4. forEach for...in for...of

    forEach orEach 方法为数组中含有有效值的每一项执行一次 callback 函数,那些已删除(使用 delete 方法等情况)或者从未赋值的项将被跳过(不包括那些值为 undefined ...

  5. react-router-dom和本地服务本地开发 (node、webpack)

    场景 使用react 做开发,避免会使用react-router React Router 已经是V4的版本 React Router 目前已经被划分成了三个包:react-router,react- ...

  6. Linux awk基础笔记

    正则表达式含义与构成:用单个字符串来描述或者匹配一系列符合某个句法规则的字符串,一般是由普通字符与特殊字符组成 awk 选项 '模式或者命令{编辑指令}' 文件名 awk '//{print}' aw ...

  7. thinkphp5 toArray()报错

    //DB操作返回是数组.模型直接操作返回是对象 //对象类型转换数组 //打开 database.php 增加或修改参数 'resultset_type' => '\think\Collecti ...

  8. 关于instanceof的使用

    测试单独一个类: <?php class A { } $a = new A(); if($a instanceof A){ echo "对象\$a实现了A类"; } 当一个子 ...

  9. java 代理模式(模拟代购)

    interface Isubject{ void buyAJ(); } class realsubject implements Isubject{ public void buyAJ(){ Syst ...

  10. 在window10平台下安装TensorFlow(only cpu)

    这是我在安装tensorflow遇到的问题记录 希望可以给大家一些帮助(2019年1月6日) 1. 需要安装的环境及软件 python3.6 Anaconda Tensorflow 2. 先安装ana ...