【发布时间】:2020-03-03 05:41:38
【问题描述】:
为什么我试图打印“reference_wrapper for string”的行对不受支持的操作员给出错误
int main(){
int s= 43;
string str = "hello";
reference_wrapper<int> x{s};
reference_wrapper<string> y{str};
x.get() = 47;
y.get() = "there";
cout<<"printing original int "<<s<<"\n";
cout<<"printing original string "<<str<<"\n";
cout<<"printing reference_wrapper for int "<<x<<"\n";
cout<<"printing reference_wrapper for string "<<y<<"\n"; // gives error
int& refint = x;
string& refstr = y;
cout<<"printing reference for int "<<refint<<"\n";
cout<<"printing reference for string "<<refstr<<"\n";
}
【问题讨论】:
标签: c++ templates reference-wrapper