Type traits | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Compile-time rational arithmetic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Compile-time integer sequences | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(C++14) |
Defined in header <type_traits> | ||
template<class...T> struct common_reference; | (since C++20) | |
Determines the common reference type of the typesT...
, that is, the type to which all the types inT...
can be converted or bound. If such a type exists (as determined according to the rules below), the membertype
names that type. Otherwise, there is no membertype
. The behavior is undefined if any of the types inT...
is an incomplete type other than (possibly cv-qualified)void.
When given reference types,common_reference
attempts to find a reference type to which the supplied reference types can all be bound, but may return a non-reference type if it cannot find such a reference type.
type
.T...
contains only one typeT0
), the membertype
names the same type asT0.T...
contains two typesT1
andT2
):S
be thesimple common reference type ofT1
andT2
(as defined below). The member typetype
namesS
if all of the conditions below are satisfied:T1
andT2
are both reference typesS
is well-formed
| (since C++23) |
TiQ
is a unary alias template such thatTiQ<U> isU
with the addition ofTi
's cv- and reference qualifiers, then the member typetype
names that type;val
is a function templatetemplate<class T> T val();, is a valid type, then the member typetype
names that type;type
names that type;type
.T...
consists of the typesT1, T2, R...
), then ifstd::common_reference_t<T1, T2> exists, the membertype
denotesstd::common_reference_t<std::common_reference_t<T1, T2>, R...> if such a type exists. In all other cases, there is no membertype
.Thesimple common reference type of two reference typesT1
andT2
is defined as follows:
T1
iscv1 X&
andT2
iscv2 Y&
(i.e., both are lvalue reference types): their simple common reference type isdecltype(false?std::declval<cv12 X&>():std::declval<cv12 Y&>()), wherecv12 is the union ofcv1 andcv2, if that type exists and is a reference type;T1
andT2
are both rvalue reference types: if the simple common reference type ofT1&
andT2&
(determined according to the previous bullet) exists, then letC
denote that type's corresponding rvalue reference type. Ifstd::is_convertible_v<T1, C> andstd::is_convertible_v<T2, C> are bothtrue, then the simple common reference type ofT1
andT2
isC
;A&
and the other must be an rvalue reference typeB&&
(A
andB
might be cv-qualified). LetD
denote the simple common reference type ofA& andBconst&, if any. IfD exists andstd::is_convertible_v<B&&, D> istrue, then the simple common reference type isD
;SeeConditional operator for the definition of the type of expressionfalse? X: Y like the ones used above.
Contents |
Name | Definition |
type | the common reference type for allT... |
template<class...T> using common_reference_t= std::common_reference<T...>::type; | ||
template<class T,class U,template<class>class TQual,template<class>class UQual> struct basic_common_reference{}; | ||
The class templatebasic_common_reference
is a customization point that allows users to influence the result ofcommon_reference
for user-defined types (typically proxy references). The primary template is empty.
A program may specializestd::basic_common_reference<T, U, TQual, UQual> on the first two parametersT
andU
ifstd::is_same_v<T,std::decay_t<T>> andstd::is_same_v<U,std::decay_t<U>> are bothtrue and at least one of them depends on a program-defined type.
If such a specialization has a member namedtype
, it must be a public and unambiguous member that names a type to which bothTQual<T> andUQual<U> are convertible. Additionally,std::basic_common_reference<T, U, TQual, UQual>::type andstd::basic_common_reference<U, T, UQual, TQual>::type must denote the same type.
A program may not specializebasic_common_reference
on the third or fourth parameters, nor may it specializecommon_reference
itself. A program that adds specializations in violation of these rules has undefined behavior.
The standard library provides following specializations ofbasic_common_reference
:
determines the common reference type of twopair s(class template specialization)[edit] | |
determines the common reference type of atuple and atuple-like type(class template specialization)[edit] | |
determines the common reference type ofreference_wrapper and non-reference_wrapper (class template specialization)[edit] |
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_common_reference | 202302L | (C++23) | Makestd::common_reference_t ofstd::reference_wrapper a reference type |
#include <concepts>#include <type_traits> static_assert(std::same_as<int&, std::common_reference_t<std::add_lvalue_reference_t<int>,std::add_lvalue_reference_t<int>&,std::add_lvalue_reference_t<int>&&,std::add_lvalue_reference_t<int>const,std::add_lvalue_reference_t<int>const&>>); int main(){}
(C++11) | determines the common type of a group of types (class template)[edit] |
(C++20) | specifies that two types share a common reference type (concept)[edit] |