gpt4 book ai didi

c++ - boost::bind 和对临时变量的引用

转载 作者:搜寻专家 更新时间:2023-10-31 00:46:41 27 4
gpt4 key购买 nike

假设我有方法:

void foo(const std::string& s);

我可以创建 boost::function:

boost::function<void(const std::string&)> f = boost::bind(foo, temp);

其中 temp 是在调用 f 之前删除的 char*。

最佳答案

是的。 Bind 不知道 char* 可以保存在一个字符串中,或​​者它正在传递给一个字符串。要避免这种情况,请使用:

boost::bind(foo, std::string(temp));

这样您的临时文件就会作为字符串复制到 Binder 中。

关于c++ - boost::bind 和对临时变量的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4863211/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com