- 浏览: 265275 次
- 性别:
- 来自: 武汉
文章分类
最新评论
-
daknife:
谢谢你的这篇文章,让我大概了解了select的一部分底层原理。 ...
Linux-2.6.25 select系统调用源码分析 -
gjlzjb:
非常有用,谢谢哈。另外问下,您是否用过Pheonix Syst ...
Why Map/Reduce? -
zhangyafei_kimi:
canbo 写道请问,我怎么生成安装包,提供给其它用户安装呢? ...
下载最新的Google Chrome源码并编译 -
canbo:
请问,我怎么生成安装包,提供给其它用户安装呢?
下载最新的Google Chrome源码并编译
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
发表评论
-
The Elements of Programing Style
2009-08-09 18:26 1268把代码写清楚,别耍小聪明。 想干什么,讲的简单点、直接点。 只 ... -
6个变态的C语言Hello World程序
2009-06-01 09:37 807转载自:http://cocre.com/?p=914 下面 ... -
在VS2005中使用IBM Purify的注意事项
2009-05-12 12:24 3991Rational Purify 使用及分析实例可以见这里htt ... -
boost.pool源码整理和使用说明
2007-07-22 13:49 270Source #ifndef __KIMI_BOOST_PO ... -
一个STL风格的动态二维数组
2007-07-22 18:05 1524#ifndef __KIMI_BOOST_ARRAY2#def ... -
boost.any源码整理和使用说明
2007-08-24 22:44 2088Source #include <algorithm& ... -
boost.array源码整理和使用说明
2007-08-24 22:45 1291Source #include <cstddef> ... -
boost.BOOST_STATIC_ASSERT源码整理和使用说明
2007-08-24 22:49 1361Source #include <boost/conf ... -
boost.shared_ptr源码整理和使用说明
2007-08-24 22:51 4185Source #pragma once //share ... -
boost.lexical_cast源码整理和使用说明
2007-08-24 22:55 1543Source #include <cstddef> ... -
编译期判断类的继承性
2007-08-24 23:00 1104介绍一个雕虫小技:编译期判断类的继承性。具体来说就是类型U是否 ... -
泛型快速排序
2007-08-28 03:20 940Source #ifndef kimi_quicksort ... -
C++ Meta Programming 和 Boost MPL(1)
2007-08-30 23:01 1602本系列全部转载自kuibyshev.bokee.com ... -
C++ Meta Programming 和 Boost MPL(2)
2007-08-30 23:02 1534本系列全部转载自kuibyshev.bokee.com ... -
C++ Meta Programming 和 Boost MPL(3)
2007-08-30 23:06 1525本系列全部转载自kuibyshev.bokee.com ... -
C++ Meta Programming 和 Boost MPL(4)
2007-08-30 23:07 1700本系列全部转载自kuibyshev.bokee.com ... -
泛型归并排序
2007-09-18 00:23 1212#define SENTINEL_CARD (-1) # ... -
泛型插入排序
2007-09-18 00:25 1208#pragma once #include <iter ... -
boost.tuple源码整理和使用说明
2007-10-07 23:13 1594Introduction A tuple (or n-tup ... -
才发现VC中也可以检测内存泄漏
2009-03-30 14:54 1377#include <stdio.h> ...
相关推荐
7. **编译时计算和元编程**:Boost.MPL(元编程库)和Boost.TypeTraits允许在编译时进行计算和类型检查,对于优化控制算法和减少运行时开销非常有帮助。 8. **测试和诊断**:Boost.Test库提供了单元测试框架,帮助...
7. **泛型编程**:Boost库中还有许多用于泛型编程的工具,如Boost.MPL(元编程库)、Boost.TypeTraits和Boost.FunctionTypes等,它们可以用来编写高度抽象和高效的代码。 8. **数学与随机数**:Boost.Math库提供了...
2. **模板元编程(Template Metaprogramming, TMP)**:Boost库中的许多组件如Boost.MPL(Metaprogamming Library)和Boost.TypeTraits利用模板元编程技术,允许在编译时进行计算和类型检查,提高了程序的效率和安全...
总之,Boost库是C++开发中不可或缺的一部分,提供了大量实用的工具,而这个“boost_1_73_0编译好的静态库文件版本”是为了方便Windows 10和Visual Studio 2019用户快速集成和使用Boost库,减少了编译和配置的复杂性...
要使用Boost库,首先需要解压"boost_1_81_0.tar.gz",然后根据Boost的安装指南编译和安装。这通常涉及配置、编译和安装步骤,可能需要使用到`bjam`或`b2`构建工具。安装完成后,就可以在项目中通过`#include`指令...
6. **泛型编程**:`mpl`(元编程库)和`type_traits`提供了丰富的模板元编程工具,允许在编译时进行计算和类型检查。 7. **数学与随机数**:Boost.Math包含一些数学函数和常数值,而Boost.Random则提供了一系列高...
8. **类型识别和转换(Boost.TypeTraits)**:帮助开发者获取类型信息,判断类型是否具有某种特性,或者在不同类型之间进行安全转换。 9. **智能指针的弱引用版本(Boost.WeakPtr)**:在某些情况下,需要能够引用...
- **类型分类(type_traits)**:学习如何使用`type_traits`来检测和操作类型信息。 - **泛型指针类(any)**:研究`any`类如何实现不同类型数据的封装。 - **Tuple Types**:了解`tuple`类型如何提高数据结构的灵活性和...
- **类型分类(type_traits)**:研究了类型分类器的概念及其在Boost库中的应用案例,这对于进行模板元编程非常有帮助。 - **泛型指针(any)**:讲解了如何利用Boost.Any来存储不同类型的对象,并讨论了其内部实现机制...
4. **模板元编程**:Boost利用C++的模板元编程技术,创建了强大的库,如Type Traits和 MPL(元编程库)。 5. **测试框架**:Boost.Test提供了一套完整的单元测试框架,有助于确保MySQL-Boost的代码质量。 安装与...
- **构建**:指导如何从源码构建Boost.Regex库,包括所需的工具和步骤。 - **安装**:说明如何将构建好的库安装到系统中以便其他项目可以使用。 #### 六、简介与概述(Introduction and Overview) - 提供了关于...
该库仿效Boost,实现了包括type_traits、mpl算法在内的多种特性,并提供了容器、迭代器、内存管理、字符串处理、智能指针、容器算法以及神经网络算法(如BP和Ada-Boost网络)和遗传算法等丰富功能。
- **type_traits**:提供对类型特性的查询能力,如类型是否是整数、是否有构造函数等。 5. **多线程**(Threading): - **thread**:线程类,提供创建和管理线程的方法。 - **mutex**:互斥锁,用于同步多线程...
- **类型识别与转换**:`boost::type_traits`,用于静态类型检查和转换 - **泛型编程**:`mpl`(元编程库)和`fusion`,提供强大的模板元编程工具 - **算法**:如`foreach`,提供简洁的循环语法;`accumulators`...
boost当前最新1.57版本源代码包。 Updated Libraries: Any, Asio, Circular Buffer, Config, Container, Coroutine, Flyweight, ... Preprocessor, Thread, TypeIndex, TypeTraits, Units, Unordered, Utility, uBLAS.