开发者

boost::bind and reference to temp variable

开发者 https://www.devze.com 2023-02-07 17:49 出处:网络
Suppose I have method: void foo(const std::string& s); Can I create boost::function: boost::function<void(const std::string&)> f = boost::bind(foo, temp);

Suppose I have method:

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

Can I create boost::function:

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

where temp is char* that is deleted before f is calle开发者_JAVA百科d.


Yes. Bind cannot know that the char* can be held in a string, or that it is being passed to a string. To circumvent this, use:

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

So that your temp is copied into the binder as a string.


And this is compiling for you? It should be

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号