在下面的代码,push_back()
一个std::ref
成std::vector<reference_wrapper<Type>>
效果很好但是,分配std::ref
到reference_wrapper<Type>
不起作用。为什么?的reference_wrapper:作品的push_back但不分配
#include <iostream>
#include <vector>
#include <functional>
using namespace std;
struct Type {};
int main()
{
Type t1;
vector<reference_wrapper<Type>> t2;
t2.push_back(ref(t1)); // OK
//reference_wrapper<Type> t3; // error: no matching function for call to std::reference_wrapper<Type>::reference_wrapper()’
//t3 = ref(t1);
return 0;
}
你试图默认构造一个引用包装器,它究竟是指什么,如果这是允许的?问题的标题与错误无关。 – Praetorian
为什么不''reference_wrapper t3 = ref(t1);'? –