C++ std :: reference_wrapper如何隐式转换为引用?

Ell*_*tch 2 c++ reference implicit-conversion c++11 reference-wrapper

我最近开始使用这std::reference_wrapper门课.当替换原始引用的某些用法时,我注意到我没有必要使用该get()函数将reference_wrappers作为参数传递给采用普通引用的函数.

void foo(const T& a);
//...
T obj;
std::reference_wrapper<const T> ref(obj);
foo(ref);  //works!
//foo(ref.get()); also works, but I expected that it would be required
Run Code Online (Sandbox Code Playgroud)

std::reference_wrapper当它传递给函数时,如何隐式转换为原始引用?

Jer*_*fin 6

reference_wrapper包括(§20.8.3):

// access
operator T& () const noexcept;
Run Code Online (Sandbox Code Playgroud)

这是一个转换运算符,由于未指定为转换运算符,因此explicit它允许隐式转换reference_wrapper<T>T&.