Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::common_reference

      From cppreference.com
      <cpp‎ |types
       
       
      Metaprogramming library
      Type traits
      Type categories
      (C++11)
      (C++11)(DR*)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11) 
      Type properties
      (C++11)
      (C++11)
      (C++14)
      (C++11)(deprecated in C++26)
      (C++11)(until C++20*)
      (C++11)(deprecated in C++20)
      (C++11)
      Type trait constants
      Metafunctions
      (C++17)
      Supported operations
      Relationships and property queries
      Type modifications
      Type transformations
      (C++11)(deprecated in C++23)
      (C++11)(deprecated in C++23)
      (C++11)
      (C++11)(until C++20*)(C++17)

      common_reference
      (C++20)
      (C++11)
      (C++17)
      Compile-time rational arithmetic
      Compile-time integer sequences
       
      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.

      • Ifsizeof...(T) is zero, there is no membertype.
      • Ifsizeof...(T) is one (i.e.,T... contains only one typeT0), the membertype names the same type asT0.
      • Ifsizeof...(T) is two (i.e.,T... contains two typesT1 andT2):
        • Let typeS 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 types
          • S is well-formed
      (since C++23)
      • Otherwise, ifstd::basic_common_reference<std::remove_cvref_t<T1>,std::remove_cvref_t<T2>, T1Q, T2Q>::type exists, whereTiQ 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;
        • Otherwise, ifdecltype(false? val<T1>(): val<T2>()), whereval is a function templatetemplate<class T> T val();, is a valid type, then the member typetype names that type;
        • Otherwise, ifstd::common_type_t<T1, T2> is a valid type, then the member typetype names that type;
        • Otherwise, there is no membertype.
      • Ifsizeof...(T) is greater than two (i.e.,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:

      • IfT1 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;
      • IfT1 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;
      • Otherwise, one of the two types must be an lvalue reference typeA& 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;
      • Otherwise, there's no simple common reference type.

      SeeConditional operator for the definition of the type of expressionfalse? X: Y like the ones used above.

      Contents

      [edit]Member types

      Name Definition
      type the common reference type for allT...

      [edit]Helper types

      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.

      [edit]Specializations

      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 twopairs
      (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]

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_common_reference202302L(C++23)Makestd::common_reference_t ofstd::reference_wrapper a reference type

      [edit]Examples

      Run this code
      #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(){}

      [edit]See also

      determines the common type of a group of types
      (class template)[edit]
      specifies that two types share a common reference type
      (concept)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/types/common_reference&oldid=170267"

      [8]ページ先頭

      ©2009-2025 Movatter.jp