On this page
std::common_type
Defined in header <type_traits> |
||
|---|---|---|
|
(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 membertype. - If
sizeof...(T)is one (i.e.,T...contains only one typeT0), the membertypenames the same type asstd::common_type<T0, T0>::typeif it exists; otherwise there is no membertype. - If
sizeof...(T)is two (i.e.,T...contains exactly two typesT1andT2), - If applying
std::decayto at least one ofT1andT2produces a different type, the membertypenames the same type asstd::common_type<std::decay<T1>::type, std::decay<T2>::type>::type, if it exists; if not, there is no membertype; - Otherwise, if there is a user specialization for
std::common_type<T1, T2>, that specialization is used; - Otherwise, if
std::decay<decltype(false ? std::declval<T1>() : std::declval<T2>())>::typeis a valid type, the membertypedenotes that type, see the conditional operator;
|
(since C++20) |
- Otherwise, there is no member
type. - If
sizeof...(T)is greater than two (i.e.,T...consists of the typesT1, T2, R...), then ifstd::common_type<T1, T2>::typeexists, the membertypedenotesstd::common_type<typename std::common_type<T1, T2>::type, R...>::typeif such a type exists. In all other cases, there is no membertype.
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.
If an instantiation of a template above depends, directly or indirectly, on an incomplete type, and that instantiation could yield a different result if that type were hypothetically completed, the behavior is undefined.
Member types
| Name | Definition |
|---|---|
type |
the common type for all T... |
Helper types
|
(since C++14) |
Specializations
Users may specialize common_type for types T1 and T2 if
- At least one of
T1andT2depends on a user-defined type, and std::decayis an identity transformation for bothT1andT2.
If such a specialization has a member named type, it must be a public and unambiguous member 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 (except for std::basic_common_reference)(since C++20) from <type_traits> is undefined.
The following specializations are already provided by the standard library:
|
(C++11)
|
specializes the std::common_type trait (class template specialization) |
|
(C++11)
|
specializes the std::common_type trait (class template specialization) |
|
(C++23)
|
determines the common type of two pairs (class template specialization) |
|
(C++23)
|
determines the common type of a tuple and a tuple-like type (class template specialization) |
|
(C++23)
|
determines the common type of an iterator and an adapted basic_const_iterator type (class template specialization) |
Possible implementation
|
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().
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>
constexpr Number<std::common_type_t<T, U>>
operator+(const Number<T>& lhs,
const Number<U>& rhs)
{
return {lhs.n + rhs.n};
}
void describe(const char* expr, const Number<int>& x)
{
std::cout << expr << " is Number<int>{" << x.n << "}\n";
}
void describe(const char* expr, const Number<double>& x)
{
std::cout << expr << " is Number<double>{" << x.n << "}\n";
}
int main()
{
Number<int> i1 = {1}, i2 = {2};
Number<double> d1 = {2.3}, d2 = {3.5};
describe("i1 + i2", i1 + i2);
describe("i1 + d2", i1 + d2);
describe("d1 + i2", d1 + i2);
describe("d1 + d2", d1 + d2);
}
Output:
i1 + i2 is Number<int>{3}
i1 + d2 is Number<double>{4.5}
d1 + i2 is Number<double>{4.3}
d1 + d2 is Number<double>{5.8}
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 2141 | C++11 | common_type<int, int>::type is int&& |
decayed result type |
| LWG 2408 | C++11 | common_type is not SFINAE-friendly |
made SFINAE-friendly |
| LWG 2460 | C++11 | common_type specializations are nearly impossible to write |
reduced number of specializations needed |
See also
|
(C++20)
|
specifies that two types share a common type (concept) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/types/common_type