【问题标题】: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));
      

      【讨论】:

        猜你喜欢
        • 2013-07-09
        • 2012-09-27
        • 2013-01-15
        • 1970-01-01
        • 1970-01-01
        • 2016-03-16
        • 2011-07-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多