std::reference_wrapper
![]() |
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <functional> 中定义
|
||
template< class T > class reference_wrapper; |
(since C++11) | |
std::reference_wrapper
是一个CopyConstructible
和CopyAssignable
的包装对象或引用的类型T
的参考。 std::reference_wrapper
实例对象(可以被复制或存储在容器中),但它们是隐式转换T&,使它们可以被用来作为参数的功能,采取通过引用基础类型.std::reference_wrapper
is a CopyConstructible
and CopyAssignable
wrapper around a reference to object or reference to function of type T
. Instances of std::reference_wrapper
are objects (can be copied or stored in containers), but they are implicitly convertible to T&, so that they can be used as arguments with the functions that take the underlying type by reference.std::reference_wrapper
对象. std::reference_wrapper
objects. std::reference_wrapper
也可用于std::bind或参考std::thread的构造函数来传递对象.std::reference_wrapper
is also used to pass objects to std::bind or to the constructor of std::thread by reference.目录 |
[编辑] 会员类型
类型
|
definition |
type
|
T
|
result_type
|
T 如果T 的返回类型是一个函数。否则,没有定义 原文: The return type of T if T is a function. Otherwise, not defined |
argument_type
|
1)如果
T 是一个函数或函数指针,它有一个参数的类型A1 ,然后argument_type 是A1 .2)如果 T 是一个类类型的成员类型T::argument_type ,然后argument_type 是一个别名原文: 1) if T is a function or pointer to function that takes one argument of type A1 , then argument_type is A1 .2) if T is a class type with a member type T::argument_type , then argument_type is an alias of that |
first_argument_type
|
1)如果
T 是一个函数或指针的函数,它接受两个参数类型为SA1 和A2 ,然后first_argument_type 是A1 .2)如果 原文: 1) if T is a function or pointer to function that takes two arguments of type s A1 and A2 , then first_argument_type is A1 .2) if |
second_argument_type
|
1)如果
T 是一个函数或指针的函数,它接受两个参数类型为SA1 和A2 ,然后second_argument_type 是A2 .2)如果 T 是一个类类型的成员类型T::second_argument_type ,然后first_argument_type 是一个别名原文: 1) if T is a function or pointer to function that takes two arguments of type s A1 and A2 , then second_argument_type is A2 .2) if T is a class type with a member type T::second_argument_type , then first_argument_type is an alias of that |
[编辑] 成员函数
存储一个参考在一个新std::reference_wrapper对象 原文: stores a reference in a new std::reference_wrapper object (公共成员函数) | |
重新绑定std::reference_wrapper (公共成员函数) | |
访问所存储的参考 (公共成员函数) | |
调用存储功能 (公共成员函数) |
[编辑] 示例
#include <algorithm> #include <list> #include <vector> #include <iostream> #include <functional> int main() { std::list<int> l = {-4, -3, -2, -1, 0, 1, 2, 3, 4}; std::vector<std::reference_wrapper<int>> v(l.begin(), l.end()); std::random_shuffle(v.begin(), v.end()); std::vector<std::reference_wrapper<int>> v2(v.begin(), v.end()); std::partition(v2.begin(), v2.end(), [](int n){return n<0;}); std::cout << "Contents of the list: "; for(int n: l) { std::cout << n << ' '; } std::cout << '\n'; std::cout << "Contents of the list, shuffled: "; for(int i: v) { std::cout << i << ' '; } std::cout << '\n'; std::cout << "Shuffled elements, partitioned: "; for(int i: v2) { std::cout << i << ' '; } std::cout << '\n'; }
输出:
Contents of the list: -4 -3 -2 -1 0 1 2 3 4 Contents of the list, shuffled: 0 -1 3 4 -4 1 -2 -3 2 Shuffled elements, partitioned: -3 -1 -2 -4 4 1 3 0 2
[编辑] 另请参阅
(C++11) (C++11) |
创建类型从参数可推导的std::reference_wrapper (函数模板) |
(C++11) |
对函数对象绑定一个或多个参数 原文: binds one or more arguments to a function object (函数模板) |