`
zhangyafei_kimi
  • 浏览: 265275 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

boost.type_traits源码整理和使用说明(1)

 
阅读更多

Introduction

The Boost type-traits library contains a set of very specific traits classes, each of which encapsulate a single trait from the C++ type system; for example, is a type a pointer or a reference type? Or does a type have a trivial constructor, or a const-qualifier?

The type-traits classes share a unified design: each class inherits from a the type true_type if the type has the specified property and inherits from false_type otherwise.

The type-traits library also contains a set of classes that perform a specific transformation on a type; for example, they can remove a top-level const or volatile qualifier from a type. Each class that performs a transformation defines a single typedef-member type that is the result of the transformation.

The boost type_traits library is partly finished in "Macro" form, so it is difficult to read. I rearrange the source code in a clear form.

I will divide the boost type_traits library into four parts to study and publish.

Type Tranformers

boost::add_const

Add appropriate const attribute to a type.

template< typename T >

struct add_const

{typedef T const type;};

template< typename T >

struct add_const<T&>

{typedef T& type;};

template< typename T >

struct add_const<T*>

{typedef T* const type;};

template< typename T >

struct add_const<T const>

{typedef T const type;};

Expression Result type

add_const<int>::type int const

add_const<int&>::type int&

add_const<int*>::type int* const

add_const<int const>::type int const

boost::add_cv

Add appropriate const volatile attribute to a type.

The source and situation is the same as boost::add_const and omitted.

Expression Result type

add_const<int>::type int const volatile

add_const<int&>::type int&

add_const<int*>::type int* const volatile

add_const<int const>::type int const volatile

boost::add_pointer

Get the appropriate pointer type of a input type.

template <typename T>

struct add_pointer_impl

{

typedef T* type;

};

template <typename T>

struct add_pointer_impl<T&>

{

typedef T* type;

};

template <typename T>

struct add_pointer_impl<T&const>

{

typedef T* type;

};

template <typename T>

struct add_pointer_impl<T&volatile>

{

typedef T* type;

};

template <typename T>

struct add_pointer_impl<T&const volatile>

{

typedef T* type;

};

template <typename T>

struct add_pointer_impl

{

typedef typename remove_reference<T>::type no_ref_type;

typedef no_ref_type* type;

};

template<typename T>

struct add_pointer

{

typedef typename add_pointer_impl<T>::type type;

};

Expression Result Type

add_pointer<int>::type int*

add_pointer<int const&>::type int const*

add_pointer<int*>::type int**

add_pointer<int*&>::type int**

boost::add_reference

If T is not a reference type then T&, otherwise T.

Note that C++ does not support reference to reference.

//T is a non-reference type, T& is the result

template <bool x>

struct reference_adder

{

template <typename T> struct result_

{

typedef T& type;

};

};

//T is a reference type, so T is the result

template <>

struct reference_adder<true>

{

template <typename T> struct result_

{

typedef T type;

};

};

template <typename T>

struct add_reference_impl

{

typedef typename reference_adder<::boost::is_reference<T>::value>::

template result_<T> result_struct;

typedef typename result_struct::type type;

};

//partial specialization for reference type

template< typename T >

struct add_reference_impl<T&>

{typedef T& type;};

//partial specialization for void, void const, void volatile, void const volatile

template<>

struct add_reference_impl<void>

{typedef void type;};

template<>

struct add_reference_impl<void const>

{typedef void const type;};

template<>

struct add_reference_impl<void volatile>

{typedef void volatile type;};

template<>

struct add_reference_impl<void const volatile>

{typedef void const volatile type;};

template< typename T >

struct add_reference

{

typedef typename add_reference_impl<T>::type

type;

};

Expression Result Type

add_reference<int>::type int&

add_reference<int const&>::type int const&

add_reference<int*>::type int*&

add_reference<int*&>::type int*&

boost::add_volatile

The same type as T volatile for all T.

Note that C++ does not support a volatile reference. It is meaningless.

//T to T volatile

template< typename T >

struct add_volatile

{typedef T volatile type;};

//T& to T&

template< typename T >

struct add_volatile<T&>

{typedef T& type;};

Expression Result Type

add_volatile<int>::type int volatile

add_volatile<int&>::type int&

add_volatile<int*>::type int* volatile

add_volatile<int const>::type int const volatile

boost::remove_all_extents

Remove all array attribute.

//a wrapper that provide the interface of typedef

template< typename T >

struct remove_all_extents

{typedef T type;};

//1d array to its raw type

template< typename T, std::size_t N >

struct remove_all_extents<T[N]>

{typedef typename boost::remove_all_extents<T>::type type;};

template< typename T, std::size_t N >

struct remove_all_extents<T const[N]>

{typedef typename boost::remove_all_extents<T const>::type type;};

template< typename T, std::size_t N >

struct remove_all_extents<T volatile[N]>

{typedef typename boost::remove_all_extents<T volatile>::type type;};

template< typename T, std::size_t N >

struct remove_all_extents<T const volatile[N]>

{typedef typename boost::remove_all_extents<T const volatile>::type type;};

//n-d array to (n-1)-d array(n>=2)

template< typename T >

struct remove_all_extents<T[]>

{typedef typename boost::remove_all_extents<T>::type type;};

template< typename T >

struct remove_all_extents<T const[]>

{typedef typename boost::remove_all_extents<T const>::type type;};

template< typename T >

struct remove_all_extents<T volatile[]>

{typedef typename boost::remove_all_extents<T volatile>::type type;};

template< typename T >

struct remove_all_extents<T const volatile[]>

Expression Result Type

remove_all_extents<int>::type int

remove_all_extents<int const[2]>::type int const

remove_all_extents<int[][2]>::type int

remove_all_extents<int[2][3][4]>::type int

remove_all_extents<int const*>::type int const*

boost::remove_const

Any top level const-qualifier will be removed. There is no "T const& to T&" or "T const * to T*", but "T const to T" and "T const volatile to T volatile" Are provided.

cv_traits_imp::is_const is not used in this module.

//T*, const T*, volatile T*, const volatile T* to T

template <typename T> struct cv_traits_imp {};

template <typename T>

struct cv_traits_imp<T*>

{

static const bool is_const = false;

static const bool is_volatile = false;

typedef T unqualified_type;

};

template <typename T>

struct cv_traits_imp<const T*>

{

static const bool is_const = true;

static const bool is_volatile = false;

typedef T unqualified_type;

};

template <typename T>

struct cv_traits_imp<volatile T*>

{

static const bool is_const = false;

static const bool is_volatile = true;

typedef T unqualified_type;

};

template <typename T>

struct cv_traits_imp<const volatile T*>

{

static const bool is_const = true;

static const bool is_volatile = true;

typedef T unqualified_type;

};

//non-volatile type

template <typename T, bool is_vol>

struct remove_const_helper

{

typedef T type;

};

//为了把T const volatile转换成T volatile,必须加此中间层

template <typename T>

struct remove_const_helper<T, true>

{

typedef T volatile type;

};

<font

分享到:
评论

相关推荐

    Boost_ClosedLoop_boost控制_boost闭环_boost闭环_Boost_boost双闭环_源码.zip

    7. **编译时计算和元编程**:Boost.MPL(元编程库)和Boost.TypeTraits允许在编译时进行计算和类型检查,对于优化控制算法和减少运行时开销非常有帮助。 8. **测试和诊断**:Boost.Test库提供了单元测试框架,帮助...

    boost_1_59_0.7z

    7. **泛型编程**:Boost库中还有许多用于泛型编程的工具,如Boost.MPL(元编程库)、Boost.TypeTraits和Boost.FunctionTypes等,它们可以用来编写高度抽象和高效的代码。 8. **数学与随机数**:Boost.Math库提供了...

    boost编程.zip

    2. **模板元编程(Template Metaprogramming, TMP)**:Boost库中的许多组件如Boost.MPL(Metaprogamming Library)和Boost.TypeTraits利用模板元编程技术,允许在编译时进行计算和类型检查,提高了程序的效率和安全...

    boost_1_73_0编译好的静态库文件版本

    总之,Boost库是C++开发中不可或缺的一部分,提供了大量实用的工具,而这个“boost_1_73_0编译好的静态库文件版本”是为了方便Windows 10和Visual Studio 2019用户快速集成和使用Boost库,减少了编译和配置的复杂性...

    boost_1_81_0.tar.gz

    要使用Boost库,首先需要解压"boost_1_81_0.tar.gz",然后根据Boost的安装指南编译和安装。这通常涉及配置、编译和安装步骤,可能需要使用到`bjam`或`b2`构建工具。安装完成后,就可以在项目中通过`#include`指令...

    boost库1.65.1源码

    6. **泛型编程**:`mpl`(元编程库)和`type_traits`提供了丰富的模板元编程工具,允许在编译时进行计算和类型检查。 7. **数学与随机数**:Boost.Math包含一些数学函数和常数值,而Boost.Random则提供了一系列高...

    boost-caffe依赖库.rar

    8. **类型识别和转换(Boost.TypeTraits)**:帮助开发者获取类型信息,判断类型是否具有某种特性,或者在不同类型之间进行安全转换。 9. **智能指针的弱引用版本(Boost.WeakPtr)**:在某些情况下,需要能够引用...

    c++资源Boost库及指南

    - **类型分类(type_traits)**:学习如何使用`type_traits`来检测和操作类型信息。 - **泛型指针类(any)**:研究`any`类如何实现不同类型数据的封装。 - **Tuple Types**:了解`tuple`类型如何提高数据结构的灵活性和...

    Boost库学习指南.pdf

    - **类型分类(type_traits)**:研究了类型分类器的概念及其在Boost库中的应用案例,这对于进行模板元编程非常有帮助。 - **泛型指针(any)**:讲解了如何利用Boost.Any来存储不同类型的对象,并讨论了其内部实现机制...

    mysql-boost-8.0.27.tar.gz

    4. **模板元编程**:Boost利用C++的模板元编程技术,创建了强大的库,如Type Traits和 MPL(元编程库)。 5. **测试框架**:Boost.Test提供了一套完整的单元测试框架,有助于确保MySQL-Boost的代码质量。 安装与...

    boost regex帮助文档

    - **构建**:指导如何从源码构建Boost.Regex库,包括所需的工具和步骤。 - **安装**:说明如何将构建好的库安装到系统中以便其他项目可以使用。 #### 六、简介与概述(Introduction and Overview) - 提供了关于...

    基于C++的extl模板库设计与实现源码

    该库仿效Boost,实现了包括type_traits、mpl算法在内的多种特性,并提供了容器、迭代器、内存管理、字符串处理、智能指针、容器算法以及神经网络算法(如BP和Ada-Boost网络)和遗传算法等丰富功能。

    Boost程序库完全开发指南——深入C++“准”标准库高清版

    - **type_traits**:提供对类型特性的查询能力,如类型是否是整数、是否有构造函数等。 5. **多线程**(Threading): - **thread**:线程类,提供创建和管理线程的方法。 - **mutex**:互斥锁,用于同步多线程...

    Boost程序库完全开发指南 第3版 中文 完整目录

    - **类型识别与转换**:`boost::type_traits`,用于静态类型检查和转换 - **泛型编程**:`mpl`(元编程库)和`fusion`,提供强大的模板元编程工具 - **算法**:如`foreach`,提供简洁的循环语法;`accumulators`...

    boost1.57源代码

    boost当前最新1.57版本源代码包。 Updated Libraries: Any, Asio, Circular Buffer, Config, Container, Coroutine, Flyweight, ... Preprocessor, Thread, TypeIndex, TypeTraits, Units, Unordered, Utility, uBLAS.

Global site tag (gtag.js) - Google Analytics