Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::add_lvalue_reference,std::add_rvalue_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
      add_lvalue_referenceadd_rvalue_reference
      (C++11)(C++11)

      Type transformations
      (C++11)(deprecated in C++23)
      (C++11)(deprecated in C++23)
      (C++11)
      (C++11)(until C++20*)(C++17)

      Compile-time rational arithmetic
      Compile-time integer sequences
       
      Defined in header<type_traits>
      template<class T>
      struct add_lvalue_reference;
      (1)(since C++11)
      template<class T>
      struct add_rvalue_reference;
      (2)(since C++11)

      Creates an lvalue or rvalue reference type ofT.

       Type trait The type referred by the nested typetype
       T is areferenceable type  T is not a referenceable type 
      (1)T&[1]T
      (2)T&&[2]
      1. This rule reflects the semantics ofreference collapsing.
      2. This rule reflects the semantics ofreference collapsing. Note thatstd::add_rvalue_reference<T&>::type isT&, which is not an rvalue reference type.

      If the program adds specializations for any of the templates described on this page, the behavior is undefined.

      Contents

      [edit]Nested types

      Name Definition
      type determined as above

      [edit]Helper types

      template<class T>
      using add_lvalue_reference_t=typename add_lvalue_reference<T>::type;
      (since C++14)
      template<class T>
      using add_rvalue_reference_t=typename add_rvalue_reference<T>::type;
      (since C++14)

      [edit]Notes

      The major difference to directly usingT& orT&& is thatT can be a non-referenceable type. For example,std::add_lvalue_reference<void>::type isvoid, whilevoid& leads to a compilation error.

      [edit]Possible implementation

      namespace detail{template<class T>struct type_identity{using type= T;};// or use std::type_identity (since C++20) template<class T>// Note that “cv void&” is a substitution failureauto try_add_lvalue_reference(int)-> type_identity<T&>;template<class T>// Handle T = cv void caseauto try_add_lvalue_reference(...)-> type_identity<T>; template<class T>auto try_add_rvalue_reference(int)-> type_identity<T&&>;template<class T>auto try_add_rvalue_reference(...)-> type_identity<T>;}// namespace detail template<class T>struct add_lvalue_reference: decltype(detail::try_add_lvalue_reference<T>(0)){}; template<class T>struct add_rvalue_reference: decltype(detail::try_add_rvalue_reference<T>(0)){};

      [edit]Example

      Run this code
      #include <type_traits> using non_ref=int;static_assert(std::is_lvalue_reference_v<non_ref>==false); using l_ref= std::add_lvalue_reference_t<non_ref>;static_assert(std::is_lvalue_reference_v<l_ref>==true); using r_ref= std::add_rvalue_reference_t<non_ref>;static_assert(std::is_rvalue_reference_v<r_ref>==true); using void_ref= std::add_lvalue_reference_t<void>;static_assert(std::is_reference_v<void_ref>==false); int main(){}

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2101C++11the program was ill-formed ifT is afunction type withcv orrefthe type produced isT in this case

      [edit]See also

      checks if a type is either anlvalue reference orrvalue reference
      (class template)[edit]
      removes a reference from the given type
      (class template)[edit]
      combinesstd::remove_cv andstd::remove_reference
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/types/add_reference&oldid=177615"

      [8]ページ先頭

      ©2009-2025 Movatter.jp