Namespaces
Variants

std:: reference_wrapper

From cppreference.net
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++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* )
定义于头文件 <functional>
template < class T >
class reference_wrapper ;
(C++11 起)

std::reference_wrapper 是一个类模板,它将引用包装成可复制、可赋值的对象。

具体而言, std::reference_wrapper 是一个 可复制构造 可复制赋值 的包装器,用于包装对类型 T 对象或函数的引用。 std::reference_wrapper 的实例是对象(它们可以被复制或存储在容器中),但它们可隐式转换为 T & ,因此可以作为参数用于那些通过引用接受底层类型的函数。

如果存储的引用是 Callable std::reference_wrapper 可使用相同参数进行调用。

辅助函数 std::ref std::cref 常用于生成 std::reference_wrapper 对象。

std::reference_wrapper 用于将对象以引用方式传递给 std::bind std::thread 的构造函数,或辅助函数 std::make_pair std::make_tuple 。它也可用作在标准容器(如 std::vector )中存储引用的机制,这些容器通常无法直接持有引用。

std::reference_wrapper 被保证是 可平凡复制的

(C++17 起)

T 可以是非完整类型。

(since C++20)

目录

成员类型

类型 定义
type T
result_type
(C++17 中弃用)
(C++20 中移除)
T 是函数类型,则为 T 的返回类型。否则未定义。
argument_type
(C++17 中弃用)
(C++20 中移除)
  • T 是接受一个 A1 类型参数的函数或函数指针,则 argument_type A1
  • T 是接受零个参数的类 T0 的成员函数指针,则 argument_type T0 * (可能带有 cv 限定符)
  • T 是具有成员类型 T :: argument_type 的类类型,则 argument_type 是该类型的别名
first_argument_type
(C++17 中弃用)
(C++20 中移除)
  • T 是接受两个 A1 A2 类型参数的函数或函数指针,则 first_argument_type A1
  • T 是接受一个参数的类 T0 的成员函数指针,则 first_argument_type T0 * (可能带有 cv 限定符)
  • T 是具有成员类型 T :: first_argument_type 的类类型,则 first_argument_type 是该类型的别名
second_argument_type
(C++17 中弃用)
(C++20 中移除)
  • T 是接受两个 A1 A2 类型参数的函数或函数指针,则 second_argument_type A2
  • T 是接受一个 A1 类型参数的类 T0 的成员函数指针,则 second_argument_type A1 (可能带有 cv 限定符)
  • T 是具有成员类型 T :: second_argument_type 的类类型,则 second_argument_type 是该类型的别名

成员函数

在新建的 std::reference_wrapper 对象中存储引用
(公开成员函数)
重新绑定 std::reference_wrapper
(公开成员函数)
访问存储的引用
(公开成员函数)
调用存储的函数
(公开成员函数)

非成员函数

比较 reference_wrapper 对象所存储的引用
(函数)

推导指引 (since C++17)

辅助类

确定 reference_wrapper 与非 reference_wrapper 的公共引用类型
(类模板特化)

可能的实现

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:
    // 类型定义
    using type = T;
    // 构造/复制/销毁
    template<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;
    // 赋值操作
    reference_wrapper& operator=(const reference_wrapper& x) noexcept = default;
    // 访问操作
    constexpr operator T& () const noexcept { return *_ptr; }
    constexpr T& get() const noexcept { return *_ptr; }
    template<class... ArgTypes>
    constexpr std::invoke_result_t<T&, ArgTypes...>
        operator() (ArgTypes&&... args ) const
            noexcept(std::is_nothrow_invocable_v<T&, ArgTypes...>)
    {
        return std::invoke(get(), std::forward<ArgTypes>(args)...);
    }
private:
    T* _ptr;
};
// 推导指引
template<class T>
reference_wrapper(T&) -> reference_wrapper<T>;

示例

演示了将 std::reference_wrapper 用作引用容器的用法,这使得可以通过多个索引访问同一容器。

#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <random>
#include <vector>
void println(auto const rem, std::ranges::range auto const& v)
{
    for (std::cout << rem; auto const& 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 vector
    std::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);
}

可能的输出:

Contents of the list: -4 -3 -2 -1 0 1 2 3 4 5
Contents of the list, as seen through a shuffled vector: -1 2 -2 1 5 0 3 -3 -4 4
Doubling 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

参见

(C++11) (C++11)
创建从实参推导类型的 std::reference_wrapper
(函数模板)
(C++11)
绑定一个或多个实参到函数对象
(函数模板)
获取包装在 std::reference_wrapper 中的引用类型
(类模板)