common_type

| Home | Libraries | People | FAQ | More

### common_type

`#include <boost/type_traits/common_type.hpp>`

    namespace boost {
      template <class ...T>  struct common_type;
    }

common_type is a traits class used to deduce a type common to a several types, useful as the return type of functions operating on multiple input types such as in mixed-mode arithmetic..

The nested typedef `::type` could be defined as follows:

    template <class ...T>
    struct common_type;

    template <class T, class U, class ...V>
    struct common_type<T,U,...V> {
        typedef typename __common_type__<typename __common_type__<T, U>::type, V...>::type type;
    };

    template <class T>
    struct common_type<T> {
        typedef T type;
    };

    template <class T, class U>
    struct common_type<T, U> {
        typedef decltype(__declval__<bool>() ? __declval__<T>() : __declval__<U>()) type;
    };

All parameter types must be complete. This trait is permitted to be specialized by a user if at least one template parameter is a user-defined type. Note: Such specializations are required when only explicit conversions are desired among the common_type arguments.

##### Configuration macros

When the compiler does not support static assertions then the user can select the way static assertions are reported. Define

- BOOST_COMMON_TYPE_USES_STATIC_ASSERT: define it if you want to use Boost.StaticAssert
- BOOST_COMMON_TYPE_USES_MPL_ASSERT: define it if you want to use Boost.MPL static asertions

The default behavior is to use mpl assertions in this case, but setting BOOST_COMMON_TYPE_USES_STATIC_ASSERT may reduce compile times and header dependencies somewhat.

Depending on the static assertion used you will have an hint of the failing assertion either through the symbol or through the text.

When possible common_type is implemented using `decltype`. Otherwise when BOOST_COMMON_TYPE_DONT_USE_TYPEOF is not defined it uses Boost.TypeOf.