std::reference_wrapper
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
| Defined in header <functional>
|
||
| template< class T > class reference_wrapper; |
(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.You can help to correct and verify the translation. Click here for instructions.
std::reference_wrapper对象. You can help to correct and verify the translation. Click here for instructions.
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.You can help to correct and verify the translation. Click here for instructions.
目录 |
[编辑] 会员类型
| 类型
Original: type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
definition |
type
|
T
|
result_type
|
T如果T的返回类型是一个函数。否则,没有定义 Original: The return type of T if T is a function. Otherwise, not defined The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
argument_type
|
1)如果
T是一个函数或函数指针,它有一个参数的类型A1,然后argument_type是A1.2)如果 T是一个类类型的成员类型T::argument_type,然后argument_type是一个别名Original: 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 thatThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
first_argument_type
|
1)如果
T是一个函数或指针的函数,它接受两个参数类型为SA1和A2,然后first_argument_type是A1.2)如果 Original: 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 The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
second_argument_type
|
1)如果
T是一个函数或指针的函数,它接受两个参数类型为SA1和A2,然后second_argument_type是A2.2)如果 T是一个类类型的成员类型T::second_argument_type,然后first_argument_type是一个别名Original: 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 thatThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
[编辑] 成员函数
| 存储一个参考在一个新std::reference_wrapper对象 Original: stores a reference in a new std::reference_wrapper object The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
| 重新绑定std::reference_wrapper Original: rebinds a std::reference_wrapper The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
| 访问所存储的参考 Original: accesses the stored reference The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
| 调用存储功能 Original: calls the stored function The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
[编辑] 为例
You can help to correct and verify the translation. Click here for instructions.
#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'; }
Output:
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推导出它的参数类型 Original: creates a std::reference_wrapper with a type deduced from its argument The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) |
| (C++11) |
绑定一个或多个参数的函数对象 Original: binds one or more arguments to a function object The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) |