std::experi...common_type(3)
π NAME π
Bestie, welcome to std::experi...common_type(3) from the C++ Standard Libary manual. The NAME entry is literally std::experimental::ranges::common_type β std::experimental::ranges::common_type, same on both sides, and that's iconic. π
β¨ Synopsis β¨
Defined in header <experimental/ranges/type_traits>.
template< class... T > (ranges TS)
struct common_type;
Okay so this template is lowkey the friend who figures out the common type among all the types in T... β that is, the type that all of T... can be implicitly converted to. If such a type exists (as determined according to the rules below, no cap), the member type names that type. Otherwise, there is no member type. Also, huge caveat: the behavior is undefined if any of the types in T... is an incomplete type other than (possibly cv-qualified) void. Not me warning you twice. π
The vibe check rules are:
- If
sizeof...(T)is zero, there is no member type. - If
sizeof...(T)is one (i.e.,T...contains only one type,T0), the member type names the same type asstd::decay_t<T0>. - If
sizeof...(T)is two (i.e.,T...contains exactly two types,T1andT2):- If applying
std::decayto at least one ofT1andT2produces a different type, the member type names the same type asranges::common_type_t<std::decay_t<T1>, std::decay_t<T2>>, if it exists; if not, there is no member type. - Otherwise, (and unless there is a user specialization for
ranges::common_type<T1, T2>), ifstd::common_type_t<T1, T2>is well-formed, then the member type denotes that type. - Otherwise, the member type denotes the type
std::decay_t<decltype(false ? std::declval<const T1&>() : std::declval<const T2&>())>, if that conditional expression is well-formed; if not, there is no member type.
- If applying
- If
sizeof...(T)is greater than two (i.e.,T...consists of the typesT1,T2,R...), then ifranges::common_type_t<T1, T2>exists, the member type denotesranges::common_type_t<ranges::common_type_t<T1, T2>, R...>if such a type exists. In all other cases, there is no member type.
π Member types π
One member type, and it's giving main character energy:
- Name:
typeβ Definition: the common type for allT....
π¦ Helper types π¦
template< class... T >
using common_type_t = typename common_type<T...>::type;
πΈ 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 type that names a cv-unqualified non-reference type to which both T1 and T2 are explicitly convertible. Additionally, ranges::common_type_t<T1, T2> and ranges::common_type_t<T2, T1> must denote the same type.
π¨ A program that adds common_type specializations in violation of these rules has undefined behavior. That's not a vibe. Periodt.
β 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().
π Example π
This section is incomplete.
Reason: no example.
π See also π
common_type(C++11) β determines the common type of a group of types (class template)common_referenceβ determine the common reference type of a set of types (class template)
π·οΈ Category π·οΈ
- Todo no example
source/version + date: http://cppreference.com Β· 2024.06.10 Β· page std::experi...common_type(3) β bye bestie, go slay some type traits π
β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms