在线文档教程
C++
应用 | Utilities

std::common_type

std::common_type

Defined in header
template< class... T > struct common_type;(since C++11)

Determines the common type among all types T..., that is the type all T... can be implicitly converted to. If such a type exists (as determined according to the rules below), the member type names that type. Otherwise, there is no member type.

  • If sizeof...(T) is zero, there is no member type.

The types in the parameter pack T shall each be a complete type, (possibly cv-qualified) void, or an array of unknown bound. Otherwise, the behavior is undefined.

Member types

NameDefinition
typethe common type for all T...

Helper types

template< class... T > using common_type_t = typename common_type::type;(since C++14)

Specializations

Users may specialize common_type for types T1 and T2 if.

  • At least one of T1 and T2 depends on a user-defined type, and

If such a specialization has a member named type, it must be a public and unambiguous member type that names a cv-unqualified non-reference type to which both T1 and T2 are explicitly convertible. Additionally, std::common_type<T1, T2>::type and std::common_type<T2, T1>::type must denote the same type.

A program that adds common_type specializations in violation of these rules has undefined behavior.

Note that the behavior of a program that adds a specialization to any other template from <type_traits> is undefined.

The following specializations are already provided by the standard library:

std::common_typespecializes the std::common_type trait (class template specialization)
std::common_type<std::chrono::time_point>specializes the std::common_type trait (class template specialization)

Possible implementation

| // primary template (used for zero types) template <class ...T> struct common_type { }; //////// one type template <class T> struct common_type<T> { using type = std::decay_t<T>; }; //////// two types // default implementation for two types template<class T1, class T2> using cond_t = decltype(false ? std::declval<T1>() : std::declval<T2>() template<class T1, class T2, class=void> struct common_type_2_default { }; template<class T1, class T2> struct common_type_2_default<T1, T2, std::void_t<cond_t<T1, T2>>> { using type = std::decay_t<cond_t<T1, T2>>; }; // dispatcher to decay the type before applying specializations template<class T1, class T2, class D1 = std::decay_t<T1>, class D2=std::decay_t<T2>> struct common_type_2_impl : common_type<D1, D2> {}; template<class D1, class D2> struct common_type_2_impl<D1, D2, D1, D2> : common_type_2_default<D1, D2> {}; template <class T1, class T2> struct common_type<T1, T2> : common_type_2_impl<T1, T2> { }; //////// 3+ types template<class AlwaysVoid, class T1, class T2, class...R> struct common_type_multi_impl { }; template< class T1, class T2, class...R> struct common_type_multi_impl<std::void_t<common_type_t<T1, T2>>, T1, T2, R...> : common_type<common_type_t<T1, T2>, R...> { }; template <class T1, class T2, class... R> struct common_type<T1, T2, R...> : common_type_multi_impl<void, T1, T2, R...> { }; |

|:----|

Notes

For arithmetic types not subject to promotion, the common type may be viewed as the type of the (possibly mixed-mode) arithmetic expression such as T0() + T1() + ... + Tn().

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DRApplied toBehavior as publishedCorrect behavior
LWG 2141C++11common_type<int, int>::type is int&&decayed result type
LWG 2408C++11common_type is not SFINAE-friendlymade SFINAE-friendly
LWG 2460C++11common_type specializations are nearly impossible to writereduced number of specializations needed

Examples

Demonstrates mixed-mode arithmetic on a user-defined class.

#include <iostream> #include <type_traits> template <class T> struct Number { T n; }; template <class T, class U> Number<typename std::common_type<T, U>::type> operator+(const Number<T>& lhs, const Number<U>& rhs) { return {lhs.n + rhs.n}; } int main() { Number<int> i1 = {1}, i2 = {2}; Number<double> d1 = {2.3}, d2 = {3.5}; std::cout << "i1i2: " << (i1 + i2).n << "\ni1d2: " << (i1 + d2).n << '\n' << "d1i2: " << (d1 + i2).n << "\nd1d2: " << (d1 + d2).n << '\n'; }

Output:

i1i2: 3 i1d2: 4.5 d1i2: 4.3 d1d2: 5.8

© cppreference.com

Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.

http://en.cppreference.com/w/cpp/types/common_type