Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::unwrap_reference,std::unwrap_ref_decay

      From cppreference.com
      <cpp‎ |utility‎ |functional
       
       
      Utilities library
       
      Function objects
      Function invocation
      (C++17)(C++23)
      Identity function object
      (C++20)
      Reference wrappers
      (C++11)(C++11)
      unwrap_referenceunwrap_ref_decay
      (C++20)(C++20)
      Old binders and adaptors
      (until C++17*)
      (until C++17*)
      (until C++17*)
      (until C++17*)  
      (until C++17*)
      (until C++17*)(until C++17*)(until C++17*)(until C++17*)
      (until C++20*)
      (until C++20*)
      (until C++17*)(until C++17*)
      (until C++17*)(until C++17*)

      (until C++17*)
      (until C++17*)(until C++17*)(until C++17*)(until C++17*)
      (until C++20*)
      (until C++20*)
       
      Defined in header<type_traits>
      Defined in header<functional>
      template<class T>
      struct unwrap_reference;
      (1)(since C++20)
      template<class T>
      struct unwrap_ref_decay;
      (2)(since C++20)

      Unwraps anystd::reference_wrapper: changingstd::reference_wrapper<U> toU&.

      1) IfT is a specialization ofstd::reference_wrapper, unwraps it; otherwise,T remains the same.
      2) If the decayedT is a specialization ofstd::reference_wrapper, unwraps it; otherwise,T is decayed.

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

      Contents

      [edit]Nested types

      Type Definition
      type

      (1)U& ifT isstd::reference_wrapper<U>;T otherwise
      (2)U& ifstd::decay_t<T> isstd::reference_wrapper<U>;std::decay_t<T> otherwise

      [edit]Helper types

      template<class T>
      using unwrap_reference_t= unwrap_reference<T>::type;
      (1)(since C++20)
      template<class T>
      using unwrap_ref_decay_t= unwrap_ref_decay<T>::type;
      (2)(since C++20)

      [edit]Possible implementation

      template<class T>struct unwrap_reference{using type= T;};template<class U>struct unwrap_reference<std::reference_wrapper<U>>{using type= U&;}; template<class T>struct unwrap_ref_decay: std::unwrap_reference<std::decay_t<T>>{};

      [edit]Notes

      std::unwrap_ref_decay performs the same transformation as used bystd::make_pair andstd::make_tuple.

      Feature-test macroValueStdFeature
      __cpp_lib_unwrap_ref201811L(C++20)std::unwrap_ref_decay andstd::unwrap_reference

      [edit]Example

      Run this code
      #include <cassert>#include <functional>#include <iostream>#include <type_traits> int main(){    static_assert(std::is_same_v<std::unwrap_reference_t<int>,int>);    static_assert(std::is_same_v<std::unwrap_reference_t<constint>,constint>);    static_assert(std::is_same_v<std::unwrap_reference_t<int&>,int&>);    static_assert(std::is_same_v<std::unwrap_reference_t<int&&>,int&&>);    static_assert(std::is_same_v<std::unwrap_reference_t<int*>,int*>); {using T=std::reference_wrapper<int>;using X= std::unwrap_reference_t<T>;        static_assert(std::is_same_v<X,int&>);}{using T=std::reference_wrapper<int&>;using X= std::unwrap_reference_t<T>;        static_assert(std::is_same_v<X,int&>);}     static_assert(std::is_same_v<std::unwrap_ref_decay_t<int>,int>);    static_assert(std::is_same_v<std::unwrap_ref_decay_t<constint>,int>);    static_assert(std::is_same_v<std::unwrap_ref_decay_t<constint&>,int>); {using T=std::reference_wrapper<int&&>;using X= std::unwrap_ref_decay_t<T>;        static_assert(std::is_same_v<X,int&>);} {auto reset=[]<typename T>(T&& z){//  x = 0; // Error: does not work if T is reference_wrapper<>// converts T&& into T& for ordinary types// converts T&& into U& for reference_wrapper<U>            decltype(auto) r= std::unwrap_reference_t<T>(z);std::cout<<"r: "<< r<<'\n';            r=0;// OK, r has reference type}; int x=1;        reset(x);assert(x==0); int y=2;        reset(std::ref(y));assert(y==0);}}

      Output:

      r: 1r: 2

      [edit]See also

      CopyConstructible andCopyAssignable reference wrapper
      (class template)[edit]
      creates apair object of type, determined by the argument types
      (function template)[edit]
      (C++11)
      creates atuple object of the type defined by the argument types
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/functional/unwrap_reference&oldid=177524"

      [8]ページ先頭

      ©2009-2025 Movatter.jp