首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::reference_wrapper

Defined in header <functional>

?

?

template< class T > class reference_wrapper;

?

(since C++11)

std::reference_wrapper是一个类模板,它将引用包装在可复制的可分配对象中。它经常被用作在标准容器%28中存储引用的机制。std::vector%29通常不能保存引用。

特别是,std::reference_wrapperCopyConstructibleCopyAssignable对对象的引用或对类型函数的引用的包装器T...的例子std::reference_wrapper对象%28可以复制或存储在容器%29中,但它们可以隐式转换为T&,以便它们可以用作参数,并具有接受基础类型的引用函数。

辅助函数std::refstd::cref经常用于生成std::reference_wrapper物品。

std::reference_wrapper也用于将对象传递给std::bind的构造函数std::thread参考一下。

std::reference_wrapper is guaranteed to be TriviallyCopyable.

(since C++17)

成员类型

type

definition

type

T

result_type(deprecated in C++17)

The return type of T if T is a function. Otherwise, not defined

argument_type(deprecated in C++17)

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 pointer to member function of class T0 that takes no arguments, then argument_type is T0*, possibly cv-qualified 3) if T is a class type with a member type T::argument_type, then argument_type is an alias of that.

first_argument_type(deprecated in C++17)

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 T is a pointer to member function of class T0 that takes one argument, then first_argument_type is T0*, possibly cv-qualified 3) if T is a class type with a member type T::first_argument_type, then first_argument_type is an alias of that.

second_argument_type(deprecated in C++17)

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 pointer to member function of class T0 that takes one argument A1, then second_argument_type is A1, possibly cv-qualified 3) if T is a class type with a member type T::second_argument_type, then second_argument_type is an alias of that.

成员函数

(constructor)

stores a reference in a new std::reference_wrapper object (public member function)

operator=

rebinds a std::reference_wrapper (public member function)

getoperator T&

accesses the stored reference (public member function)

operator()

calls the stored function (public member function)

可能的实施

模板<class T>类引用[医]包装器{public://type tydurifT type;//struction/Copy/destruction引用[医]包装器%28t和参%29不包括:[医]ptr%28std::地址%28ref%29%29{}引用[医]包装器%28t&&lt;%29=删除;引用[医]包装器%28[医]包装器&%29 no以外=默认;//赋值引用[医]包装器和操作符=%28 const引用[医]包装器&x%29 no以外=默认;//访问操作符T&%28%29 constno,{Report]%2A[医]tr;}T&除{返回外,获取%28%29 Const no%2A[医]模板<class...。ArgTypes>TypeName STD::Result[医]类型操作符%28%29%28 ArgTypes&&...args%29(返回STD::Invoke%28get%28%29,std::Forward)<ArgTypes>%28 args%29...%29;}私有:t%2A[医]PTR;};

*。

演示引用的使用[医]包装器作为引用的容器,这使得使用多个索引访问同一个容器成为可能。

二次

代码语言:javascript
复制
#include <algorithm>
#include <list>
#include <vector>
#include <iostream>
#include <numeric>
#include <random>
#include <functional>
 
int main()
{
    std::list<int> l(10);
    std::iota(l.begin(), l.end(), -4);
 
    std::vector<std::reference_wrapper<int>> v(l.begin(), l.end());
    // can't use shuffle on a list (requires random access), but can use it on a vector
    std::shuffle(v.begin(), v.end(), std::mt19937{std::random_device{}()});
 
    std::cout << "Contents of the list: ";
    for (int n : l) std::cout << n << ' '; std::cout << '\n';
 
    std::cout << "Contents of the list, as seen through a shuffled vector: ";
    for (int i : v) std::cout << i << ' '; std::cout << '\n';
 
    std::cout << "Doubling the values in the initial list...\n";
    for (int& i : l) {
        i *= 2;
    }
 
    std::cout << "Contents of the list, as seen through a shuffled vector: ";
    for (int i : v) std::cout << i << ' '; std::cout << '\n';
}

二次

可能的产出:

二次

代码语言:javascript
复制
Contents of the list: -4 -3 -2 -1 0 1 2 3 4 5 
Contents of the list, as seen through a shuffled vector: -1 2 -2 1 5 0 3 -3 -4 4 
Doubling the values in the initial list...
Contents of the list, as seen through a shuffled vector: -2 4 -4 2 10 0 6 -6 -8 8

二次

另见

refcref (C++11)(C++11)

creates a std::reference_wrapper with a type deduced from its argument (function template)

bind (C++11)

binds one or more arguments to a function object (function template)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com