C++11中std::reference_wrapper的理解

std::reference_wrapper

  • reference_wrapper 将引用包装成一个对象,即引用的包装器;
  • reference_wrapper 是一个引用包裹器,可以包裹一个指向对象或者指向函数指针的引用,既可以通过拷贝构造,也可以通过赋值构造;
  • std::reference_wrapper的实例是一个对象,但是会隐式的转换为T&,因此std::reference_wrapper可以作为将把被其包裹类型为参数的函数的实参。【太拗口了,上原文:Instances of std::reference_wrapper are objects (they 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.】。如下面的代码中,函数func的参数类型为int,而传递给func的参数确是std::reference_wrapper类型的对象。这个特性是保证reference_wrapper对象可以作为函数实参的关键。

    void func(int param){
    std::cout << param << std::endl;
    }
    int a = 3;
    std::reference_wrapper<int> ra = a;
    func(ra);
  • 若reference_wrapper包裹的引用是可以调用的,则reference_wrapper对象也是可调用的;
  •  std::ref 和std::cref 通常用来产生一个reference_wrapper对象;
  • reference_wrapper 常通过引用传递对象给std::bind函数或者std::thread构造函数。

std::reference_wrapper可能的实现

template <class T>
class reference_wrapper {
public:
  // types
  typedef T type;

  // construct/copy/destroy
  reference_wrapper(T& ref) noexcept : _ptr(std::addressof(ref)) {}//接受使用ref直接构造
  reference_wrapper(T&&) = delete;// 说明,不可以传递右值
  reference_wrapper(const reference_wrapper&) noexcept = default;//接受使用一个reference_wrapper构造

  // assignment
  reference_wrapper& operator=(const reference_wrapper& x) noexcept = default;

  // access
  operator T& () const noexcept { return *_ptr; }// T&操作符的重载是其可以作为函数实参的关键
  T& get() const noexcept { return *_ptr; }

  template< class... ArgTypes >
  std::invoke_result_t<T&, ArgTypes...>
    operator() ( ArgTypes&&... args ) const {
    return std::invoke(get(), std::forward<ArgTypes>(args)...);
  }

private:
  T* _ptr;
};

std::reference_wrapper使用实例

#include <iostream>
#include <functional>
using namespace std;

void func(int a, int b)
{
    cout << "a = " << a <<",";
    cout << "b = " << b << endl;
}

void func1(int i)
{
    cout << "i = " << i << endl;
}

int main()
{
    // 包裹函数指针
    int x=5, y=7;
    std::reference_wrapper<void(int,int)> f0 = func;
    f0(5,7);

    auto f1 = std::ref(func);
    f1(5,7);

    // 和bind结合使用
    int i = 10;

    auto f2 = std::bind(func1, i);
    auto f3 = std::bind(func1, std::ref(i));

    i = 30;

    f2();
    f3();

    return 0;
}

//a = 5,b = 7
//a = 5,b = 7
//i = 10
//i = 30

参考

猜你喜欢

转载自blog.csdn.net/guoxiaojie_415/article/details/80031948