function template
<functional>
std::ref
reference (1) | template <class T> reference_wrapper<T> ref (T& elem) noexcept; |
---|
複製 (2) | template <class T> reference_wrapper<T> ref (reference_wrapper<T>& x) noexcept; |
---|
移動 (3) | template <class T> void ref (const T&&) = delete; |
---|
Construct reference_wrapper
引數
- elem
- An lvalue reference, whose reference is stored in the object.
- x
- A reference_wrapper object, which is copied.
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
// ref example
#include <iostream> // std::cout
#include <functional> // std::ref
int main () {
int foo (10);
auto bar = std::ref(foo);
++bar;
std::cout << foo << '\n';
return 0;
}
|
輸出
資料競爭
The initialization version (1) does not access elem, but the returned object can be used to access or modify it.
The copying version (2) accesses its argument (x), returning an object that which can be used to access or modify its referred element.