Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::reference_wrapper

      From cppreference.com
      <cpp‎ |utility‎ |functional
       
       
      Utilities library
       
      Function objects
      Function invocation
      (C++17)(C++23)
      Identity function object
      (C++20)
      Reference wrappers
      reference_wrapper
      (C++11)
      (C++11)(C++11)
      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<functional>
      template<class T>
      class reference_wrapper;
      (since C++11)

      std::reference_wrapper is a class template that wraps a reference in a copyable, assignable object.

      Specifically,std::reference_wrapper is aCopyConstructible andCopyAssignable wrapper around a reference to object or reference to function of typeT. Instances ofstd::reference_wrapper are objects (they can be copied or stored in containers) but they are implicitly convertible toT&, so that they can be used as arguments with the functions that take the underlying type by reference.

      If the stored reference isCallable,std::reference_wrapper is callable with the same arguments.

      Helper functionsstd::ref andstd::cref are often used to generatestd::reference_wrapper objects.

      std::reference_wrapper is used to pass objects by reference tostd::bind, the constructor ofstd::thread, or the helper functionsstd::make_pair andstd::make_tuple. It can also be used as a mechanism to store references inside standard containers (likestd::vector) that cannot normally hold references.

      std::reference_wrapper is guaranteed to beTriviallyCopyable.

      (since C++17)

      T may be an incomplete type.

      (since C++20)

      Contents

      [edit]Member types

      type definition
      typeT
      result_type
      (deprecated in C++17)
      (removed in C++20)
      The return type ofT ifT is a function. Otherwise, not defined.
      argument_type
      (deprecated in C++17)
      (removed in C++20)
      • ifT is a function or pointer to function that takes one argument of typeA1, thenargument_type isA1
      • ifT is a pointer to member function of classT0 that takes no arguments, thenargument_type isT0*, possibly cv-qualified
      • ifT is a class type with a member typeT::argument_type, thenargument_type is an alias of that
      first_argument_type
      (deprecated in C++17)
      (removed in C++20)
      • ifT is a function or pointer to function that takes two arguments of typesA1 andA2, thenfirst_argument_type isA1
      • ifT is a pointer to member function of classT0 that takes one argument, thenfirst_argument_type isT0*, possibly cv-qualified
      • ifT is a class type with a member typeT::first_argument_type, thenfirst_argument_type is an alias of that
      second_argument_type
      (deprecated in C++17)
      (removed in C++20)
      • ifT is a function or pointer to function that takes two arguments of type sA1 andA2, thensecond_argument_type isA2
      • ifT is a pointer to member function of classT0 that takes one argumentA1, thensecond_argument_type isA1, possibly cv-qualified
      • ifT is a class type with a member typeT::second_argument_type, thensecond_argument_type is an alias of that

      [edit]Member functions

      stores a reference in a newstd::reference_wrapper object
      (public member function)[edit]
      rebinds astd::reference_wrapper
      (public member function)[edit]
      accesses the stored reference
      (public member function)[edit]
      calls the stored function
      (public member function)[edit]

      [edit]Non-member functions

      comparesreference_wrapper objects as their stored references
      (function)[edit]

      [edit]Deduction guides(since C++17)

      [edit]Helper classes

      determines the common reference type ofreference_wrapper and non-reference_wrapper
      (class template specialization)[edit]

      [edit]Possible implementation

      namespace detail{template<class T>constexpr T& FUN(T& t)noexcept{return t;}template<class T>void FUN(T&&)= delete;} template<class T>class reference_wrapper{public:// typesusing type= T; // construct/copy/destroytemplate<class U,class= decltype(        detail::FUN<T>(std::declval<U>()),std::enable_if_t<!std::is_same_v<reference_wrapper,std::remove_cvref_t<U>>>())>constexpr reference_wrapper(U&& u)noexcept(noexcept(detail::FUN<T>(std::forward<U>(u)))): _ptr(std::addressof(detail::FUN<T>(std::forward<U>(u)))){}     reference_wrapper(const reference_wrapper&)noexcept=default; // assignment    reference_wrapper& operator=(const reference_wrapper& x)noexcept=default; // accessconstexpr operator T&()constnoexcept{return*_ptr;}constexpr T& get()constnoexcept{return*_ptr;} template<class...ArgTypes>constexprstd::invoke_result_t<T&, ArgTypes...>        operator()(ArgTypes&&...args)constnoexcept(std::is_nothrow_invocable_v<T&, ArgTypes...>){returnstd::invoke(get(),std::forward<ArgTypes>(args)...);} private:    T* _ptr;}; // deduction guidestemplate<class T>reference_wrapper(T&)-> reference_wrapper<T>;

      [edit]Example

      Demonstrates the use ofstd::reference_wrapper as a container of references, which makes it possible to access the same container using multiple indices.

      Run this code
      #include <algorithm>#include <functional>#include <iostream>#include <list>#include <numeric>#include <random>#include <vector> void println(autoconst rem, std::ranges::rangeautoconst& v){for(std::cout<< rem;autoconst& e: v)std::cout<< e<<' ';std::cout<<'\n';} int main(){std::list<int> l(10);std::iota(l.begin(), l.end(),-4); // can't use shuffle on a list (requires random access), but can use it on a vectorstd::vector<std::reference_wrapper<int>> v(l.begin(), l.end());     std::ranges::shuffle(v,std::mt19937{std::random_device{}()});     println("Contents of the list: ", l);    println("Contents of the list, as seen through a shuffled vector: ", v); std::cout<<"Doubling the values in the initial list...\n";    std::ranges::for_each(l,[](int& i){ i*=2;});     println("Contents of the list, as seen through a shuffled vector: ", v);}

      Possible output:

      Contents of the list: -4 -3 -2 -1 0 1 2 3 4 5Contents of the list, as seen through a shuffled vector: -1 2 -2 1 5 0 3 -3 -4 4Doubling the values in the initial list...Contents of the list, as seen through a shuffled vector: -2 4 -4 2 10 0 6 -6 -8 8

      [edit]See also

      (C++11)(C++11)
      creates astd::reference_wrapper with a type deduced from its argument
      (function template)[edit]
      (C++11)
      binds one or more arguments to a function object
      (function template)[edit]
      get the reference type wrapped instd::reference_wrapper
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/functional/reference_wrapper&oldid=173390"

      [8]ページ先頭

      ©2009-2025 Movatter.jp