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
![]() |
Home | Libraries | People | FAQ | More |
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>
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>
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>
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>
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>
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>
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 fromintegral_constant<std::size_t, ALIGNOF(int)>.
alignment_of<char>::typeis the typeintegral_constant<std::size_t, ALIGNOF(char)>.
alignment_of<double>::valueis an integral constant expression with valueALIGNOF(double).
alignment_of<T>::value_typeis the typestd::size_t.
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 fromintegral_constant<std::size_t, 0>.
extent<double[2][3][4], 1>::typeis the typeintegral_constant<std::size_t, 3>.
extent<int[4]>::valueis an integral constant expression that evaluates to 4.
extent<int[][2]>::valueis an integral constant expression that evaluates to 0.
extent<int[][2], 1>::valueis an integral constant expression that evaluates to 2.
extent<int*>::valueis an integral constant expression that evaluates to 0.
extent<T>::value_typeis the typestd::size_t.
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:
T is a function type, note that this is not
the same thing as a pointer to a function.
| 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>::arg |
The Nth argument type of function type F, where 1 <= N <= arity of F. |
| 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. |
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>
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>
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>
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 fromtrue_type.
has_trivial_assign<char*>::typeis the typetrue_type.
has_trivial_assign<int (*)(long)>::valueis an integral constant expression that evaluates to true.
has_trivial_assign<MyClass>::valueis an integral constant expression that evaluates to false.
has_trivial_assign<T>::value_typeis the typebool.
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 fromtrue_type.
has_trivial_constructor<char*>::typeis the typetrue_type.
has_trivial_constructor<int (*)(long)>::valueis an integral constant expression that evaluates to true.
has_trivial_constructor<MyClass>::valueis an integral constant expression that evaluates to false.
has_trivial_constructor<T>::value_typeis the typebool.
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 fromtrue_type.
has_trivial_copy<char*>::typeis the typetrue_type.
has_trivial_copy<int (*)(long)>::valueis an integral constant expression that evaluates to true.
has_trivial_copy<MyClass>::valueis an integral constant expression that evaluates to false.
has_trivial_copy<T>::value_typeis the typebool.
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 fromtrue_type.
has_trivial_destructor<char*>::typeis the typetrue_type.
has_trivial_destructor<int (*)(long)>::valueis an integral constant expression that evaluates to true.
has_trivial_destructor<MyClass>::valueis an integral constant expression that evaluates to false.
has_trivial_destructor<T>::value_typeis the typebool.
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>
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.
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 fromtrue_type.
is_abstract<abc>::typeis the typetrue_type.
is_abstract<abc const>::valueis an integral constant expression that evaluates to true.
is_abstract<T>::value_typeis the typebool.
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 fromtrue_type.
is_arithmetic<char>::typeis the typetrue_type.
is_arithmetic<double>::valueis an integral constant expression that evaluates to true.
is_arithmetic<T>::value_typeis the typebool.
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 fromtrue_type.
is_array<char[2][3]>::typeis the typetrue_type.
is_array<double[]>::valueis an integral constant expression that evaluates to true.
is_array<T>::value_typeis the typebool.
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 fromtrue_type.
is_base_of<Base, Derived>::typeis the typetrue_type.
is_base_of<Base, Derived>::valueis an integral constant expression that evaluates to true.
is_base_of<Base, Base>::valueis an integral constant expression that evaluates to false: a class is not it's own base.
is_base_of<Derived, Base>::valueis an integral constant expression that evaluates to false: the arguments are the wrong way round.
is_base_of<T>::value_typeis the typebool.
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 fromtrue_type.
is_class<MyClass const>::typeis the typetrue_type.
is_class<MyClass>::valueis an integral constant expression that evaluates to true.
is_class<MyClass&>::valueis an integral constant expression that evaluates to false.
is_class<MyClass*>::valueis an integral constant expression that evaluates to false.
is_class<T>::value_typeis the typebool.
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 fromtrue_type.
is_compound<MyEnum>::typeis the typetrue_type.
is_compound<int*>::valueis an integral constant expression that evaluates to true.
is_compound<int&>::valueis an integral constant expression that evaluates to true.
is_compound<int>::valueis an integral constant expression that evaluates to false.
is_compound<T>::value_typeis the typebool.
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 fromtrue_type.
is_const<int const volatile>::typeis the typetrue_type.
is_const<int* const>::valueis an integral constant expression that evaluates to true.
is_const<int const*>::valueis an integral constant expression that evaluates to false: the const-qualifier is not at the top level in this case.
is_const<int const&>::valueis an integral constant expression that evaluates to false: the const-qualifier is not at the top level in this case.
is_const<int>::valueis an integral constant expression that evaluates to false.
is_const<T>::value_typeis the typebool.
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,