【问题标题】:Move Semantics and R-Value References in C++ String Construction在 C++ 字符串构造中移动语义和 R 值引用
【发布时间】:2011-11-30 05:52:45
【问题描述】:

C++11 是否会在参数字符串结构中移动语义和右值引用,例如

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

例如假设声明

void do_something_with_string(const std::string &);

可以防止"abc"的冗余堆复制?

如果是这样,它会在 boost::const_string 中不必要地使用 const char 包装器 boost::cref 吗?

【问题讨论】:

  • “C++11 会移动语义和右值引用(...)”好像你忘记了问题中的右值引用!

标签: string optimization c++11 rvalue-reference move-semantics


【解决方案1】:

您不能像那样移动数据。 const_string 具有 const char* 重载的原因是因为 const_stringconst。它的设计是不可变的。因此,它可以存储通过引用也不可变的常量字符串,例如const char*:字符串文字。

std::string 不是一成不变的。即使你在它的整个生命周期内只持有它const&,它仍然不是一个不可变的字符串。因此,它必须从const char* 复制到它自己的私有缓冲区中。

【讨论】:

    猜你喜欢
    • 2013-02-14
    • 1970-01-01
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-05
    • 1970-01-01
    相关资源
    最近更新 更多