boost is_arrary 源码分析
来源:互联网 发布:淘宝无线端自定义模块 编辑:程序博客网 时间:2024/04/28 09:59
最近再看boost的源代码,看到type_traits部分,现在拿出boost::is_array的源代码进行分析。boost的版本为1.42
boost::is_array的源码如下
现在一行一行的分析
14 #ifndef BOOST_TT_IS_ARRAY_HPP_INCLUDED
15 #define BOOST_TT_IS_ARRAY_HPP_INCLUDED
是防止此文件被多次include的--可以现不关心
19 #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
20 # include <boost/type_traits/detail/yes_no_type.hpp>
21 # include <boost/type_traits/detail/wrap.hpp>
22 #endif
23
24 #include <cstddef>
25
26 // should be the last #include
27 #include <boost/type_traits/detail/bool_trait_def.hpp>
BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION 是看你的编译器是不是支持偏特化的,现在的编译器都支持,所以不对它的实现作分析。
__CODEGEARC__ 是看编译器是否是borland的
最后看下来,也就
41 BOOST_TT_AUX_BOOL_TRAIT_PARTIAL_SPEC1_1(typename T,is_array,T[],true)
42 BOOST_TT_AUX_BOOL_TRAIT_PARTIAL_SPEC1_1(typename T,is_array,T const[],true)
43 BOOST_TT_AUX_BOOL_TRAIT_PARTIAL_SPEC1_1(typename T,is_array,T volatile[],true)
44 BOOST_TT_AUX_BOOL_TRAIT_PARTIAL_SPEC1_1(typename T,is_array,T const volatile[],true)
这四个宏是分析的重点
现在对41行进行分析。
41行的代码为 BOOST_TT_AUX_BOOL_TRAIT_PARTIAL_SPEC1_1(typename T,is_array,T[],true)
122 #define BOOST_TT_AUX_BOOL_TRAIT_PARTIAL_SPEC1_1(param,trait,sp,C) /
123 template< param > struct trait< sp > /
124 BOOST_TT_AUX_BOOL_C_BASE(C) /
125 { /
126 BOOST_TT_AUX_BOOL_TRAIT_VALUE_DECL(C) /
127 }; /
有用到一下两个宏,分别如下
62 #ifndef BOOST_TT_AUX_BOOL_C_BASE
63 # define BOOST_TT_AUX_BOOL_C_BASE(C) : ::boost::integral_constant<bool,C>
64 #endif
和
51 # define BOOST_TT_AUX_BOOL_TRAIT_VALUE_DECL(C) /
52 typedef ::boost::integral_constant<bool,C> base_; /
53 using base_::value; /
最后展开为
template<typename T> struct isarray<T[]>: ::boost::integral_constant<bool,true>
{
typedef ::boost::integral_constant<bool,true> base_;
using base_::value;
}
:boost::integral_constant的定义是
18 template <class T, T val>
20 struct integral_constant : public mpl::integral_c<T, val>
21 {
22 typedef integral_constant<T,val> type;
23 };
mpl::integral_c<T, val>的定义为
39 template< bool C >
40 struct integral_c<bool, C>
41 {
42 BOOST_STATIC_CONSTANT(bool, value = C);
43 typedef integral_c_tag tag;
44 typedef integral_c type;
45 typedef bool value_type;
46 operator bool() const { return this->value; }
47 };
BOOST_STATIC_CONSTANT(bool, value = C);的定义为
377 # ifdef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
378 # define BOOST_STATIC_CONSTANT(type, assignment) enum { assignment }
379 # else
380 # define BOOST_STATIC_CONSTANT(type, assignment) static const type assignment
381 # endif
22 struct integral_c_tag { BOOST_STATIC_CONSTANT(int, value = 0); };
现在又对boost::integral_constant<bool,true>展开为
struct integral_constant: public mpc::integral_<bool,true>
{
typedef integral_constant<bool,true> type;
}
integral_constant<bool,true> 展开为
struct integral_c<bool, true>
{
static const bool value = true;
typedef static const int value = 0 tag;
typedef integral_c type;
typedef bool value_type;
operator bool() const { return this->value; }
};
最后结合isarray<T[]>的展开,可以发现,boost判定是否为array的方法是用到了模板偏特化。
但是boost实现isarrary的判断更加严谨,具有夸平台的功能,并且对多种编译器进行实现,真是好东西呀。
- boost is_arrary 源码分析
- boost::scoped_ptr 源码分析
- boost::function & boost::bind 源码分析
- boost::mpl::list 源码分析
- (二) boost::lambda 源码分析
- (三)boost::lambda源码分析
- boost 源码 ref 库分析
- boost源码分析之 BOOST_FOREACH
- function 源码分析2--boost function的源码分析
- boost::mpl::vector(MSVC)源码分析
- boost::mpl::vector(GCC)源码分析
- (一)boost::lambda源码分析
- boost::filesystem::resize_file函数的源码分析
- boost源码分析之 BOOST_FOREACH(2)
- 决策树(七)--Boost及源码分析
- function 源码分析1--boost function缩水版本实现
- boost:asio信号量signal_set源码分析及使用
- Boost源码笔记:boost::multi_array
- SQL嵌套查询和链接查询
- caused by org.hibernate.mappingexception repeated column in mapping for entity
- Oracle存储过程总结(一、基本应用)
- 彻底搞清值传递与地址传递的区别
- 当向Oracle数据库中插入数据时,如何获取行ID
- boost is_arrary 源码分析
- 用Matlab处理视频文件
- EXTJS的学习资料
- 公司正式上 Android了。
- 项目管理的三架马车
- Visual C++ 2010 0x 特性(2):the auto keyword
- 七个受用一生的心理寓言
- 学会在困境中闭嘴
- Linux系统的默认编码设置