开发者

Move Semantics and R-Value References in C++ String Construction

Will C++11 move semantics and r-value refe开发者_开发百科rences in argument string constructs such as

do_something_with_string(std::string("abc"))

assuming declaration for example

void do_something_with_string(const std::string &);

make it possible to prevent the redundant heap-copying of "abc"?

If so will it make use of the const char wrapper boost::cref in boost::const_string unneccesary?


You can't move data like that. The reason const_string has that overload for const char* is because const_string is const. It is immutable by its design. Therefore, it can store constant strings which are also immutable by reference, like a const char*: a string literal.

std::string is not immutable. Even if you only hold it by const& for the entire duration of its life, it is still not an immutable string. Therefore, it must copy from a const char* into its own private buffer.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜