【问题标题】:boost::bind and reference to temp variableboost::bind 和对临时变量的引用
【发布时间】:2011-02-01 13:34:13
【问题描述】:
假设我有方法:
void foo(const std::string& s);
我可以创建 boost::function:
boost::function<void(const std::string&)> f = boost::bind(foo, temp);
其中 temp 是在调用 f 之前删除的 char*。
【问题讨论】:
标签:
c++
boost
boost-bind
object-lifetime
boost-function
【解决方案1】:
是的。 Bind 无法知道 char* 可以保存在字符串中,或者它正在传递给字符串。要避免这种情况,请使用:
boost::bind(foo, std::string(temp));
这样您的临时文件就会作为字符串复制到活页夹中。
【解决方案2】:
这是为你编译的?应该是
boost::function<void()> f = boost::bind(foo, std::string(temp));