Boost C++ Libraries

“...one of the most highly regarded and expertly designed C++ library projects in the world.” Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

boost.png (6897 bytes) Home Libraries People FAQ More

PrevUpHomeNext

Alphabetical Reference

add_const
add_cv
add_pointer
add_reference
add_volatile
aligned_storage
alignment_of
extent
function_traits
has_nothrow_assign
has_nothrow_constructor
has_nothrow_copy
has_trivial_assign
has_trivial_constructor
has_trivial_copy
has_trivial_destructor
has_virtual_destructor
integral_constant
is_abstract
is_arithmetic
is_array
is_base_of
is_class
is_compound
is_const
is_convertible
is_empty
is_enum
is_floating_point
is_function
is_fundamental
is_integral
is_member_function_pointer
is_member_object_pointer
is_member_pointer
is_object
is_pod
is_pointer
is_polymorphic
is_same
is_scalar
is_stateless
is_reference
is_union
is_void
is_volatile
rank
remove_all_extents
remove_const
remove_cv
remove_extent
remove_pointer
remove_reference
remove_volatile
type_with_alignment

add_const

template <class T>
struct add_const
{
   typedef see-below type;
};

type: The same type as T const for all T.

C++ Standard Reference: 3.9.3.

Compiler Compatibility: If the compiler does not support partial specialization of class-templates then this template will compile, but the member type will always be the same as type T except where compiler workarounds have been applied.

Header: #include <boost/type_traits/add_const.hpp> or #include <boost/type_traits.hpp>

Examples

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

add_cv

template <class T>
struct add_cv
{
   typedef see-below type;
};

type: The same type as T const volatile for all T.

C++ Standard Reference: 3.9.3.

Compiler Compatibility: If the compiler does not support partial specialization of class-templates then this template will compile, but the member type will always be the same as type T except where compiler workarounds have been applied.

Header: #include <boost/type_traits/add_cv.hpp> or #include <boost/type_traits.hpp>

Examples

Expression Result Type
add_cv<int>::type int const volatile
add_cv<int&>::type int&
add_cv<int*>::type int* const volatile
add_cv<int const>::type int const volatile

add_pointer

template <class T>
struct add_pointer
{
   typedef see-below type;
};

type: The same type as remove_reference<T>::type*.

The rationale for this template is that it produces the same type as TYPEOF(&t), where t is an object of type T.

C++ Standard Reference: 8.3.1.

Compiler Compatibility: If the compiler does not support partial specialization of class-templates then this template will compile, but the member type will always be the same as type T except where compiler workarounds have been applied.

Header: #include <boost/type_traits/add_pointer.hpp> or #include <boost/type_traits.hpp>

Examples

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**

add_reference

template <class T>
struct add_reference
{
   typedef see-below type;
};

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

C++ Standard Reference: 8.3.2.

Compiler Compatibility: If the compiler does not support partial specialization of class-templates then this template will compile, but the member type will always be the same as type T except where compiler workarounds have been applied.

Header: #include <boost/type_traits/add_reference.hpp> or #include <boost/type_traits.hpp>

Examples

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*&

add_volatile

template <class T>
struct add_volatile
{
   typedef see-below type;
};

type: The same type as T volatile for all T.

C++ Standard Reference: 3.9.3.

Compiler Compatibility: If the compiler does not support partial specialization of class-templates then this template will compile, but the member type will always be the same as type T except where compiler workarounds have been applied.

Header: #include <boost/type_traits/add_volatile.hpp> or #include <boost/type_traits.hpp>

Examples

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

aligned_storage

template <std::size_t Size, std::size_t Align>
struct aligned_storage
{
   typedef see-below type;
};

type: a built-in or POD type with size Size and an alignment that is a multiple of Align.

Header: #include <boost/type_traits/aligned_storage.hpp> or #include <boost/type_traits.hpp>

alignment_of

template <class T>
struct alignment_of : public integral_constant<std::size_t, ALIGNOF(T)> {};

Inherits: Class template alignment_of inherits from integral_constant<std::size_t, ALIGNOF(T)>, where ALIGNOF(T) is the alignment of type T.

Note: strictly speaking you should only rely on the value of ALIGNOF(T) being a multiple of the true alignment of T, although in practice it does compute the correct value in all the cases we know about.

Header: #include <boost/type_traits/alignment_of.hpp> or #include <boost/type_traits.hpp>

Examples:

alignment_of<int> inherits from integral_constant<std::size_t, ALIGNOF(int)>.

alignment_of<char>::type is the type integral_constant<std::size_t, ALIGNOF(char)>.

alignment_of<double>::value is an integral constant expression with value ALIGNOF(double).

alignment_of<T>::value_type is the type std::size_t.

extent

template <class T, std::size_t N = 0>
struct extent : public integral_constant<std::size_t, EXTENT(T,N)> {};

Inherits: Class template extent inherits from integral_constant<std::size_t, EXTENT(T,N)>, where EXTENT(T,N) is the number of elements in the N'th array dimention of type T.

If T is not an array type, or if N > rank<T>::value, or if the N'th array bound is incomplete, then EXTENT(T,N) is zero.

Header: #include <boost/type_traits/extent.hpp> or #include <boost/type_traits.hpp>

Examples:

extent<int[1]> inherits from integral_constant<std::size_t, 0>.

extent<double[2][3][4], 1>::type is the type integral_constant<std::size_t, 3>.

extent<int[4]>::value is an integral constant expression that evaluates to 4.

extent<int[][2]>::value is an integral constant expression that evaluates to 0.

extent<int[][2], 1>::value is an integral constant expression that evaluates to 2.

extent<int*>::value is an integral constant expression that evaluates to 0.

extent<T>::value_type is the type std::size_t.

function_traits

template <class T>
struct function_traits
{
   static const std::size_t    arity = see-below;
   typedef see-below           result_type;
   typedef see-below           argN_type; 
};

The class template function_traits will only compile if:

  • The compiler supports partial specialization of class templates.
  • The template argument T is a function type, note that this is not the same thing as a pointer to a function.

Function Traits Members

Member Description
function_traits<T>::arity An integral constant expression that gives the number of arguments accepted by the function type F.
function_traits<T>::result_type The type returned by function type F.
function_traits<T>::argN_type The Nth argument type of function type F, where 1 <= N <= arity of F.

Examples

Expression Result
function_traits<void (void)>::arity An integral constant expression that has the value 0.
function_traits<long (int)>::arity An integral constant expression that has the value 1.
function_traits<long (int, long, double, void*)>::arity An integral constant expression that has the value 4.
function_traits<void (void)>::result_type The type void.
function_traits<long (int)>::result_type The type long.
function_traits<long (int)>::arg0_type The type int.
function_traits<long (int, long, double, void*)>::arg3_type The type void*.
function_traits<long (int, long, double, void*)>::arg4_type A compiler error: there is no arg4_type since there are only three arguments.
function_traits<long (*)(void)>::arity A compiler error: argument type is a function pointer, and not a function type.

has_nothrow_assign

template <class T>
struct has_nothrow_assign : public true_type-or-false_type {};

Inherits: If T is a (possibly cv-qualified) type with a non-throwing assignment-operator then inherits from true_type, otherwise inherits from false_type. Type T must be a complete type.

Compiler Compatibility: If the compiler does not support partial-specialization of class templates, then this template can not be used with function types.

Without some (as yet unspecified) help from the compiler, has_nothrow_assign will never report that a class or struct has a non-throwing assignment-operator; this is always safe, if possibly sub-optimal. Currently (May 2005) only Visual C++ 8 has the necessary compiler support to ensure that this trait "just works".

Header: #include <boost/type_traits/has_nothrow_assign.hpp> or #include <boost/type_traits.hpp>

has_nothrow_constructor

template <class T>
struct has_nothrow_constructor : public true_type-or-false_type {};

Inherits: If T is a (possibly cv-qualified) type with a non-throwing default-constructor then inherits from true_type, otherwise inherits from false_type. Type T must be a complete type.

Compiler Compatibility: If the compiler does not support partial-specialization of class templates, then this template can not be used with function types.

Without some (as yet unspecified) help from the compiler, has_nothrow_constructor will never report that a class or struct has a non-throwing default-constructor; this is always safe, if possibly sub-optimal. Currently (May 2005) only Visual C++ 8 has the necessary compiler intrinsics to ensure that this trait "just works".

Header: #include <boost/type_traits/has_nothrow_constructor.hpp> or #include <boost/type_traits.hpp>

has_nothrow_copy

template <class T>
struct has_nothrow_copy : public true_type-or-false_type {};

Inherits: If T is a (possibly cv-qualified) type with a non-throwing copy-constructor then inherits from true_type, otherwise inherits from false_type. Type T must be a complete type.

Compiler Compatibility: If the compiler does not support partial-specialization of class templates, then this template can not be used with function types.

Without some (as yet unspecified) help from the compiler, has_nothrow_copy will never report that a class or struct has a non-throwing copy-constructor; this is always safe, if possibly sub-optimal. Currently (May 2005) only Visual C++ 8 has the necessary compiler intrinsics to ensure that this trait "just works".

Header: #include <boost/type_traits/has_nothrow_copy.hpp> or #include <boost/type_traits.hpp>

has_trivial_assign

template <class T>
struct has_trivial_assign : public true_type-or-false_type {};

Inherits: If T is a (possibly cv-qualified) type with a trivial assignment-operator then inherits from true_type, otherwise inherits from false_type.

If a type has a trivial assignment-operator then the operator has the same effect as copying the bits of one object to the other: calls to the operator can be safely replaced with a call to memcpy.

Compiler Compatibility: If the compiler does not support partial-specialization of class templates, then this template can not be used with function types.

Without some (as yet unspecified) help from the compiler, has_trivial_assign will never report that a user-defined class or struct has a trivial constructor; this is always safe, if possibly sub-optimal. Currently (May 2005) only MWCW 9 and Visual C++ 8 have the necessary compiler intrinsics to detect user-defined classes with trivial constructors.

C++ Standard Reference: 12.8p11.

Header: #include <boost/type_traits/has_trivial_assign.hpp> or #include <boost/type_traits.hpp>

Examples:

has_trivial_assign<int> inherits from true_type.

has_trivial_assign<char*>::type is the type true_type.

has_trivial_assign<int (*)(long)>::value is an integral constant expression that evaluates to true.

has_trivial_assign<MyClass>::value is an integral constant expression that evaluates to false.

has_trivial_assign<T>::value_type is the type bool.

has_trivial_constructor

template <class T>
struct has_trivial_constructor : public true_type-or-false_type {};

Inherits: If T is a (possibly cv-qualified) type with a trivial default-constructor then inherits from true_type, otherwise inherits from false_type.

If a type has a trivial default-constructor then the constructor have no effect: calls to the constructor can be safely omitted. Note that using meta-programming to omit a call to a single trivial-constructor call is of no benefit whatsoever. However, if loops and/or exception handling code can also be omitted, then some benefit in terms of code size and speed can be obtained.

Compiler Compatibility: If the compiler does not support partial-specialization of class templates, then this template can not be used with function types.

Without some (as yet unspecified) help from the compiler, has_trivial_constructor will never report that a user-defined class or struct has a trivial constructor; this is always safe, if possibly sub-optimal. Currently (May 2005) only MWCW 9 and Visual C++ 8 have the necessary compiler intrinsics to detect user-defined classes with trivial constructors.

C++ Standard Reference: 12.1p6.

Header: #include <boost/type_traits/has_trivial_constructor.hpp> or #include <boost/type_traits.hpp>

Examples:

has_trivial_constructor<int> inherits from true_type.

has_trivial_constructor<char*>::type is the type true_type.

has_trivial_constructor<int (*)(long)>::value is an integral constant expression that evaluates to true.

has_trivial_constructor<MyClass>::value is an integral constant expression that evaluates to false.

has_trivial_constructor<T>::value_type is the type bool.

has_trivial_copy

template <class T>
struct has_trivial_copy : public true_type-or-false_type {};

Inherits: If T is a (possibly cv-qualified) type with a trivial copy-constructor then inherits from true_type, otherwise inherits from false_type.

If a type has a trivial copy-constructor then the constructor has the same effect as copying the bits of one object to the other: calls to the constructor can be safely replaced with a call to memcpy.

Compiler Compatibility: If the compiler does not support partial-specialization of class templates, then this template can not be used with function types.

Without some (as yet unspecified) help from the compiler, has_trivial_copy will never report that a user-defined class or struct has a trivial constructor; this is always safe, if possibly sub-optimal. Currently (May 2005) only MWCW 9 and Visual C++ 8 have the necessary compiler intrinsics to detect user-defined classes with trivial constructors.

C++ Standard Reference: 12.8p6.

Header: #include <boost/type_traits/has_trivial_copy.hpp> or #include <boost/type_traits.hpp>

Examples:

has_trivial_copy<int> inherits from true_type.

has_trivial_copy<char*>::type is the type true_type.

has_trivial_copy<int (*)(long)>::value is an integral constant expression that evaluates to true.

has_trivial_copy<MyClass>::value is an integral constant expression that evaluates to false.

has_trivial_copy<T>::value_type is the type bool.

has_trivial_destructor

template <class T>
struct has_trivial_destructor : public true_type-or-false_type {};

Inherits: If T is a (possibly cv-qualified) type with a trivial destructor then inherits from true_type, otherwise inherits from false_type.

If a type has a trivial destructor then the destructor has no effect: calls to the destructor can be safely omitted. Note that using meta-programming to omit a call to a single trivial-constructor call is of no benefit whatsoever. However, if loops and/or exception handling code can also be omitted, then some benefit in terms of code size and speed can be obtained.

Compiler Compatibility: If the compiler does not support partial-specialization of class templates, then this template can not be used with function types.

Without some (as yet unspecified) help from the compiler, has_trivial_destructor will never report that a user-defined class or struct has a trivial destructor; this is always safe, if possibly sub-optimal. Currently (May 2005) only MWCW 9 and Visual C++ 8 have the necessary compiler intrinsics to detect user-defined classes with trivial constructors.

C++ Standard Reference: 12.4p3.

Header: #include <boost/type_traits/has_trivial_destructor.hpp> or #include <boost/type_traits.hpp>

Examples:

has_trivial_destructor<int> inherits from true_type.

has_trivial_destructor<char*>::type is the type true_type.

has_trivial_destructor<int (*)(long)>::value is an integral constant expression that evaluates to true.

has_trivial_destructor<MyClass>::value is an integral constant expression that evaluates to false.

has_trivial_destructor<T>::value_type is the type bool.

has_virtual_destructor

template <class T>
struct has_virtual_destructor : public true_type-or-false_type {};

Inherits: If T is a (possibly cv-qualified) type with a virtual destructor then inherits from true_type, otherwise inherits from false_type.

Compiler Compatibility: This trait is provided for completeness, since it's part of the Technical Report on C++ Library Extensions. However, there is currently no way to portably implement this trait. The default version provided always inherits from false_type, and has to be explicitly specialized for types with virtual destructors unless the compiler used has compiler intrinsics that enable the trait to do the right thing: currently (May 2005) only Visual C++ 8 has the necessary intrinsics.

C++ Standard Reference: 12.4.

Header: #include <boost/type_traits/has_virtual_destructor.hpp> or #include <boost/type_traits.hpp>

integral_constant

template <class T, T val>
struct integral_constant
{
   typedef integral_constant<T, val>  type;
   typedef T                          value_type;
   static const T value = val;
};

typedef integral_constant<bool, true>  true_type;
typedef integral_constant<bool, false> false_type;

Class template integral_constant is the common base class for all the value-based type traits. The two typedef's true_type and false_type are provided for convenience: most of the value traits are Boolean properties and so will inherit from one of these.

is_abstract

template <class T>
struct is_abstract : public true_type-or-false_type {};

Inherits: If T is a (possibly cv-qualified) abstract type then inherits from true_type, otherwise inherits from false_type.

C++ Standard Reference: 10.3.

Header: #include <boost/type_traits/is_abstract.hpp> or #include <boost/type_traits.hpp>

Compiler Compatibility: The compiler must support DR337 (as of April 2005: GCC 3.4, VC++ 7.1 (and later), Intel C++ 7 (and later), and Comeau 4.3.2). Otherwise behaves the same as is_polymorphic; this is the "safe fallback position" for which polymorphic types are always regarded as potentially abstract. The macro BOOST_NO_IS_ABSTRACT is used to signify that the implementation is buggy, users should check for this in their own code if the "safe fallback" is not suitable for their particular use-case.

Examples:

Given: class abc{ virtual ~abc() = 0; };

is_abstract<abc> inherits from true_type.

is_abstract<abc>::type is the type true_type.

is_abstract<abc const>::value is an integral constant expression that evaluates to true.

is_abstract<T>::value_type is the type bool.

is_arithmetic

template <class T>
struct is_arithmetic : public true_type-or-false_type {};

Inherits: If T is a (possibly cv-qualified) arithmetic type then inherits from true_type, otherwise inherits from false_type. Arithmetic types include integral and floating point types (see also is_integral and is_floating_point).

C++ Standard Reference: 3.9.1p8.

Header: #include <boost/type_traits/is_arithmetic.hpp> or #include <boost/type_traits.hpp>

Examples:

is_arithmetic<int> inherits from true_type.

is_arithmetic<char>::type is the type true_type.

is_arithmetic<double>::value is an integral constant expression that evaluates to true.

is_arithmetic<T>::value_type is the type bool.

is_array

template <class T>
struct is_array : public true_type-or-false_type {};

Inherits: If T is a (possibly cv-qualified) array type then inherits from true_type, otherwise inherits from false_type.

C++ Standard Reference: 3.9.2 and 8.3.4.

Header: #include <boost/type_traits/is_array.hpp> or #include <boost/type_traits.hpp>

Compiler Compatibility: If the compiler does not support partial-specialization of class templates, then this template can give the wrong result with function types.

Examples:

is_array<int[2]> inherits from true_type.

is_array<char[2][3]>::type is the type true_type.

is_array<double[]>::value is an integral constant expression that evaluates to true.

is_array<T>::value_type is the type bool.

is_base_of

template <class Base, class Derived>
struct is_base_of : public true_type-or-false_type {};

Inherits: If Base is base class of type Derived then inherits from true_type, otherwise inherits from false_type.

This template will detect non-public base classes, and ambiguous base classes.

Note that a class is not considered to be it's own base class, likewise, if either Base or Derived are non-class types, then the class will always inherit from false_type.

Types Base and Derived must not be incomplete types.

C++ Standard Reference: 10.

Header: #include <boost/type_traits/is_base_of.hpp> or #include <boost/type_traits.hpp>

Compiler Compatibility: If the compiler does not support partial-specialization of class templates, then this template can not be used with function types. There are some older compilers which will produce compiler errors if Base is a private base class of Derived, or if Base is an ambiguous base of Derived. These compilers include Borland C++, older versions of Sun Forte C++, Digital Mars C++, and older versions of EDG based compilers.

Examples:

Given: class Base{}; class Derived : public Base{};

is_base_of<Base, Derived> inherits from true_type.

is_base_of<Base, Derived>::type is the type true_type.

is_base_of<Base, Derived>::value is an integral constant expression that evaluates to true.

is_base_of<Base, Base>::value is an integral constant expression that evaluates to false: a class is not it's own base.

is_base_of<Derived, Base>::value is an integral constant expression that evaluates to false: the arguments are the wrong way round.

is_base_of<T>::value_type is the type bool.

is_class

template <class T>
struct is_class : public true_type-or-false_type {};

Inherits: If T is a (possibly cv-qualified) class type then inherits from true_type, otherwise inherits from false_type.

C++ Standard Reference: 3.9.2 and 9.2.

Header: #include <boost/type_traits/is_class.hpp> or #include <boost/type_traits.hpp>

Compiler Compatibility: Without (some as yet unspecified) help from the compiler, we cannot distinguish between union and class types, as a result this type will erroneously inherit from true_type for union types. See also is_union. Currently (May 2005) only Visual C++ 8 has the necessary compiler intrinsics to correctly identify union types, and therefore make is_class function correctly.

Examples:

Given: class MyClass; then:

is_class<MyClass> inherits from true_type.

is_class<MyClass const>::type is the type true_type.

is_class<MyClass>::value is an integral constant expression that evaluates to true.

is_class<MyClass&>::value is an integral constant expression that evaluates to false.

is_class<MyClass*>::value is an integral constant expression that evaluates to false.

is_class<T>::value_type is the type bool.

is_compound

template <class T>
struct is_compound : public true_type-or-false_type {};

Inherits: If T is a (possibly cv-qualified) compound type then inherits from true_type, otherwise inherits from false_type. Any type that is not a fundamental type is a compound type (see also is_fundamental).

C++ Standard Reference: 3.9.2.

Header: #include <boost/type_traits/is_compound.hpp> or #include <boost/type_traits.hpp>

Examples:

is_compound<MyClass> inherits from true_type.

is_compound<MyEnum>::type is the type true_type.

is_compound<int*>::value is an integral constant expression that evaluates to true.

is_compound<int&>::value is an integral constant expression that evaluates to true.

is_compound<int>::value is an integral constant expression that evaluates to false.

is_compound<T>::value_type is the type bool.

is_const

template <class T>
struct is_const : public true_type-or-false_type {};

Inherits: If T is a (top level) const-qualified type then inherits from true_type, otherwise inherits from false_type.

C++ Standard Reference: 3.9.3.

Header: #include <boost/type_traits/is_const.hpp> or #include <boost/type_traits.hpp>

Examples:

is_const<int const> inherits from true_type.

is_const<int const volatile>::type is the type true_type.

is_const<int* const>::value is an integral constant expression that evaluates to true.

is_const<int const*>::value is an integral constant expression that evaluates to false: the const-qualifier is not at the top level in this case.

is_const<int const&>::value is an integral constant expression that evaluates to false: the const-qualifier is not at the top level in this case.

is_const<int>::value is an integral constant expression that evaluates to false.

is_const<T>::value_type is the type bool.

is_convertible

template <class From, class To>
struct is_convertible : public true_type-or-false_type {};

Inherits: If an imaginary lvalue of type From is convertible to type To then inherits from true_type, otherwise inherits from false_type.

Type From must not be an incomplete type.

Type To must not be an incomplete, or function type.

No types are considered to be convertible to array types or abstract-class types.

This template can not detect whether a converting-constructor is public or not: if type To has a private converting constructor from type From then instantiating is_convertible<From, To> will produce a compiler error. For this reason is_convertible can not be used to determine whether a type has a public copy-constructor or not.

This template will also produce compiler errors if the conversion is ambiguous, for example:

struct A {};
struct B : A {};
struct C : A {};
struct D : B, C {};
// This produces a compiler error, the conversion is ambiguous:
bool const y = boost::is_convertible<D*,A*>::value;

C++ Standard Reference: 4 and 8.5.

Compiler Compatibility: This template is currently broken with Borland C++ Builder 5 (and earlier), for constructor-based conversions, and for the Metrowerks 7 (and earlier) compiler in all cases. If the compiler does not support is_abstract, then the template parameter To must not be an abstract type.

Header: #include <boost/type_traits/is_convertible.hpp> or #include <boost/type_traits.hpp>

Examples:

is_convertible<int,</