【发布时间】:2015-08-04 13:42:46
【问题描述】:
我发现使用 std::bind 时按引用传递往往不起作用。这是一个例子。
int test;
void inc(int &i)
{
i++;
}
int main() {
test = 0;
auto i = bind(inc, test);
i();
cout<<test<<endl; // Outputs 0, should be 1
inc(test);
cout<<test<<endl; // Outputs 1
return 0;
}
为什么通过std bind创建的函数调用时变量不递增?
【问题讨论】:
-
使用
std::ref传递对 std::bind 的引用!