【发布时间】:2013-05-14 11:48:34
【问题描述】:
我正在尝试了解 std::ref 的工作原理。
#include <functional>
#include <iostream>
template <class C>
void func(C c){
c += 1;
}
int main(){
int x{3};
std::cout << x << std::endl;
func(x);
std::cout << x << std::endl;
func(std::ref(x));
std::cout << x << std::endl;
}
Output : 3 3 4
在上面的代码中,我认为第三个函数调用的模板参数C被实例化为std::reference_wrapper<int>。
在阅读the reference时,
我注意到std::reference_wrapper<int> 中没有+= 运算符。
那么,c += 1; 是如何有效的呢?
【问题讨论】: