【发布时间】:2014-08-11 18:33:57
【问题描述】:
基本上,我希望有以下语义:
#include <functional>
#include <iostream>
class test
{
public:
void add(std::function<void()> f)
{
f();
}
void operator()()
{
++x;
}
int x = 33;
};
int main()
{
test t;
t.add(t);
// wanted: x == 34 instead: x == 33 since add(t) copies it
}
我了解 std::function 包装了可调用对象的副本,但有什么方法可以使用 std::function 获取对可调用对象的引用?
【问题讨论】:
-
请用您正在使用的编程语言标记您的问题,以便它显示在相应的列表中。
-
+1 对于
std::ref()这样一个很好的例子
标签: c++ c++11 std-function