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

This is an old version of boost. Click here for the latest version's documentation home page.
PrevUpHomeNext

Type Traits by Category

Type Traits that Describe the Properties of a Type
Type Traits that Transform One Type to Another
Synthesizing Types with Specific Alignments
Decomposing Function Types

Type Traits that Describe the Properties of a Type

These traits are all value traits, which is to say the traits classes all inherit from integral_constant, and are used to access some numerical property of a type. Often this is a simple true or false Boolean value, but in a few cases may be some other integer value (for example when dealing with type alignments, or array bounds: see alignment_of, rank and extent).

Categorizing a Type

These traits identify what "kind" of type some type T is. These are split into two groups: primary traits which are all mutually exclusive, and composite traits that are compositions of one or more primary traits.

For any given type, exactly one primary type trait will inherit from true_type, and all the others will inherit from false_type, in other words these traits are mutually exclusive.

This means that is_integral<T>::value and is_floating_point<T>::value will only ever be true for built-in types; if you want to check for a user-defined class type that behaves "as if" it is an integral or floating point type, then use the std::numeric_limits template instead.

Synopsis:

template <class T>
structis_array<T>;
  
template <class T>
structis_class<T>;
  
template <class T>
structis_enum<T>;
  
template <class T>
structis_floating_point<T>;
  
template <class T>
structis_function<T>;

template <class T>
structis_integral<T>;
  
template <class T>
structis_member_function_pointer<T>;
  
template <class T>
structis_member_object_pointer<T>;
  
template <class T>
structis_pointer<T>;
  
template <class T>
structis_reference<T>;
  
template <class T>
structis_union<T>;
  
template <class T>
structis_void<T>;

The following traits are made up of the union of one or more type categorizations. A type may belong to more than one of these categories, in addition to one of the primary categories.

template <class T>
structis_arithmetic;

template <class T>
structis_compound;

template <class T>
structis_fundamental;

template <class T>
structis_member_pointer;

template <class T>
structis_object;

template <class T>
structis_scalar;
General Type Properties

The following templates describe the general properties of a type.

Synopsis:

template <class T>
structalignment_of;

template <class T>
structhas_nothrow_assign;

template <class T>
structhas_nothrow_constructor;

template <class T>
structhas_nothrow_copy;

template <class T>
structhas_trivial_assign;

template <class T>
structhas_trivial_constructor;

template <class T>
structhas_trivial_copy;

template <class T>
structhas_trivial_destructor;

template <class T>
structhas_virtual_destructor;

template <class T>
structis_abstract;

template <class T>
structis_const;

template <class T>
structis_empty;

template <class T>
structis_stateless;

template <class T>
structis_pod;

template <class T>
structis_polymorphic;

template <class T>
structis_volatile;

template <class T, std::size_t N = 0>
structextent;

template <class T>
structrank;
Relationships Between Two Types

These templates determine the whether there is a relationship between two types:

Synopsis:

template <class Base, class Derived>
structis_base_of;

template <class From, class To>
structis_convertible;

template <class T, class U>
structis_same;

Type Traits that Transform One Type to Another

The following templates transform one type to another, based upon some well-defined rule. Each template has a single member called type that is the result of applying the transformation to the template argument T.

Synopsis:

template <class T>
structadd_const;

template <class T>
structadd_cv;

template <class T>
structadd_pointer;

template <class T>
structadd_reference;

template <class T>
structadd_volatile;

template <class T>
structremove_all_extents;

template <class T>
structremove_const;

template <class T>
structremove_cv;

template <class T>
structremove_extent;

template <class T>
structremove_pointer;

template <class T>
structremove_reference;

template <class T>
structremove_volatile;

Broken Compiler Workarounds:

For all of these templates support for partial specialization of class templates is required to correctly implement the transformation. On the other hand, practice shows that many of the templates from this category are very useful, and often essential for implementing some generic libraries. Lack of these templates is often one of the major limiting factors in porting those libraries to compilers that do not yet support this language feature. As some of these compilers are going to be around for a while, and at least one of them is very wide-spread, it was decided that the library should provide workarounds where possible.

The basic idea behind the workaround is to manually define full specializations of all type transformation templates for all fundamental types, and all their 1st and 2nd rank cv-[un]qualified derivative pointer types, and to provide a user-level macro that will define all the explicit specializations needed for any user-defined type T.

The first part guarantees the successful compilation of something like this:

BOOST_STATIC_ASSERT((is_same<char, remove_reference<char&>::type>::value));
BOOST_STATIC_ASSERT((is_same<char const, remove_reference<char const&>::type>::value));
BOOST_STATIC_ASSERT((is_same<char volatile, remove_reference<char volatile&>::type>::value));
BOOST_STATIC_ASSERT((is_same<char const volatile, remove_reference<char const volatile&>::type>::value));
BOOST_STATIC_ASSERT((is_same<char*, remove_reference<char*&>::type>::value));
BOOST_STATIC_ASSERT((is_same<char const*, remove_reference<char const*&>::type>::value));
...
BOOST_STATIC_ASSERT((is_same<char const volatile* const volatile* const volatile, remove_reference<char const volatile* const volatile* const volatile&>::type>::value));

and the second part provides the library's users with a mechanism to make the above code work not only for char, int or other built-in type, but for they own types as well:

namespace myspace{
   struct MyClass {};
}
// declare this at global scope:
BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(myspace::MyClass)
// transformations on myspace::MyClass now work:
BOOST_STATIC_ASSERT((is_same<myspace::MyClass, remove_reference<myspace::MyClass&>::type>::value));
BOOST_STATIC_ASSERT((is_same<myspace::MyClass, remove_const<myspace::MyClass const>::type>::value));
// etc.

Note that the macro BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION evaluates to nothing on those compilers that do support partial specialization.

Synthesizing Types with Specific Alignments

Some low level memory management routines need to synthesize a POD type with specific alignment properties. The template type_with_alignment finds the smallest type with a specified alignment, while template aligned_storage creates a type with a specific size and alignment.

Synopsis

template <std::size_t Align>
structtype_with_alignment;

template <std::size_t Size, std::size_t Align>
structaligned_storage;

Decomposing Function Types

The class template function_traits extracts information from function types (see also is_function). This traits class allows you to tell how many arguments a function takes, what those argument types are, and what the return type is.

Synopsis

template <std::size_t Align>
structfunction_traits;
Copyright 2000, 2005 Adobe Systems Inc, David Abrahams, Steve Cleary, Beman Dawes, Aleksey Gurtovoy, Howard Hinnant, Jesse Jones, Mat Marcus, Itay Maman, John Maddock, Thorsten Ottosen, Robert Ramey and Jeremy Siek

PrevUpHomeNext