std::
reference_wrapper::operator=
公共成员函数 <functional>
reference_wrapper& operator= (const reference_wrapper& rhs) noexcept;
|
拷贝赋值
复制rhs保存的引用,替换当前保存在*this中的引用。
调用之后,rhs和*this都指向相同的对象或函数。
☲ 参数
-
rhs
-
相同类型的reference_wrapper对象(即具有相同的模板形参T)。
☉ 返回值
*this
☣ 示例
// reference_wrapper::operator=
#include <iostream> // std::cout
#include <functional> // std::reference_wrapper
int main () {
int a,b;
// reference_wrapper assignment:
a=10; b=20;
std::reference_wrapper<int> wrap1 (a);
std::reference_wrapper<int> wrap2 (b);
wrap1 = wrap2;
++wrap1; ++wrap2;
std::cout << "a: " << a << '\n';
std::cout << "b: " << b << '\n';
std::cout << '\n';
// note the difference with:
a=10; b=20;
int& ref1 (a);
int& ref2 (b);
ref1 = ref2;
++ref1; ++ref2;
std::cout << "a: " << a << '\n';
std::cout << "b: " << b << '\n';
return 0;
}
|
输出:
a: 10
b: 22
a: 21
b: 21
⇄ 数据竞争
对象被修改。
该对象获取rhs (rhs.get())引用的元素的引用,rhs可用于访问或修改该元素。
☂ 异常安全性
无抛出保证:该成员函数从不抛出异常。
🍄 另请参阅