Namespaces
Variants

std::reference_wrapper<T>::reference_wrapper

来自 zh.cppreference.net
 
 
 
函数对象
函数调用
(C++17)(C++23)
恒等函数对象
(C++20)
透明运算符包装器
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

旧式绑定器与适配器
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)  
(直到 C++17*)
(直到 C++17*)(直到 C++17*)(直到 C++17*)(直到 C++17*)
(直到 C++20*)
(直到 C++20*)
(直到 C++17*)(直到 C++17*)
(直到 C++17*)(直到 C++17*)

(直到 C++17*)
(直到 C++17*)(直到 C++17*)(直到 C++17*)(直到 C++17*)
(直到 C++20*)
(直到 C++20*)
 
 
template< class U >
reference_wrapper( U&& x ) noexcept(/*see below*/) ;
(1) (C++11 起)
(C++20 起 constexpr)
reference_wrapper( const reference_wrapper& other ) noexcept;
(2) (C++11 起)
(C++20 起 constexpr)

构造一个新的引用包装器。

1) 将 x 转换为 T&,效果如同通过 T& t = std::forward<U>(x);,然后存储一个指向 t 的引用。仅当 typename std::decay<U>::type 与 reference_wrapper 不是同一类型,且表达式 FUN(std::declval<U>()) 是良构的时,此重载才会参与重载决议,其中 FUN 指代一组假想函数
void FUN(T&) noexcept;
void FUN(T&&) = delete;
2) 复制构造函数。存储一个指向 other.get() 的引用。

参数

x - 要包装的对象
other - 另一个引用包装器

异常

1)
noexcept 规范:
noexcept ( noexcept ( FUN ( std:: declval < U > ( ) ) ) )
其中 FUN 是上文描述中提及的虚构函数集合。

示例

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 应用于 发布时的行为 正确行为
LWG 2993 C++11 已删除的 reference_wrapper(T&&) 构造函数在某些情况下会干扰
重载决议
替换为构造函数模板