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 ...
随机推荐
- 浅谈sql之连接查询
SQL之连接查询 一.连接查询的分类 sql中将连接查询分成四类: 内链接 外连接 左外连接 右外连接 自然连接 交叉连接 二.连接查询的分类 数据库表如下: 1.学生表 2.老师表 3.班级表 表用 ...
- idea教程视频以及常用插件整理
最近在同事的强烈安利下把eclipse换成idea了,本以为需要经历一个艰难的过渡期,谁知道不到3天就深感回不去了. 哎,只能说有时候人的惰性是多么可怕! idea实在是太太太强大了. 不要再问原因. ...
- CSU - 2031 Barareh on Fire (两层bfs)
传送门: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2031 Description The Barareh village is on f ...
- Css中路径data:image/png;base64的用法详解 (转载)
大家可能注意到了,网页上有些图片的src或css背景图片的url后面跟了一大串字符,比如: background-image:url(data:image/png;base64, iVBORw0KGg ...
- springboot集成activiti工作流时容易出现的问题
No.1 启动报错 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'or ...
- 转:system.Security.Cryptography C# 加密和解密
以下文转自: http://www.360doc.com/content/13/0122/05/19147_261678471.shtml 总结:注册的时候经过MD5加密存进数据库,在登录的时候需要先 ...
- DataSet转换为泛型集合和DataRow 转成 模型类
public static class TransformToList { /// <summary> /// DataSet转换为泛型集合 /// </summary> // ...
- JavaScript深入之参数按值传递
在<JavaScript高级程序设计>第三版 4.1.3,讲到传递参数: ECMAscript中所有函数的参数都是按值传递 按值传递 也就是,把函数外部的值复制给函数内部的参数,就和把值从 ...
- mybatis调用存过程返回结果集和out参数值
Mapper文件: 1.配置一个参数映射集,采用hashMap对象 2.使用call调用存储过,其中in out等标识的参数需要有详细的描述,例如:mode,JavaType,jdbcType等 &l ...
- YII2.0 后台手动添加用户功能
后台添加管理员用户使用SignupForm类实现 步骤一.复制一份前台frontend/models/SignupForm.php 到后台模型文件夹中 backend/models/SignupFor ...