【发布时间】:2018-12-03 12:38:39
【问题描述】:
我希望stringstream 有一个从string&& 窃取其初始内容的构造函数。 STL中一般不存在这种跨物种的“移动构造函数”吗?如果没有,为什么不呢?
【问题讨论】:
-
同理,为什么
.str(string&&)不存在?
标签: c++ c++17 move-semantics stringstream rvalue-reference
我希望stringstream 有一个从string&& 窃取其初始内容的构造函数。 STL中一般不存在这种跨物种的“移动构造函数”吗?如果没有,为什么不呢?
【问题讨论】:
.str(string&&)不存在?
标签: c++ c++17 move-semantics stringstream rvalue-reference
有历史,令人失望。但也是一个看起来光明的未来。
当移动语义进入 C++11 时,它是巨大的、有争议的和压倒性的。我希望能够将字符串从stringstream 中移入和。然而,当时的政治要求内部商店不必必须成为basic_string<charT>。例如,内部存储可以是vector。并且没有使用分配器控制事物的能力。无论如何,在 C++11 时间框架内就认识到了这一需求,但这只是一座太远的桥梁。
幸运的是,Peter Sommerlad 用P0408 弥补了这一不足。该提案添加了您寻求的功能,希望用于 C++20,但这还不确定。它已成功通过 LEWG,现在在 LWG 的办公桌上。他们本月在拉珀斯维尔没有参加比赛,纯粹是因为日程安排过多。我希望它能够通过 LWG 和全体委员会的投票。它肯定会得到我的投票。
【讨论】:
为什么
std::stringstream::stringstream(std::string&&)不存在?
这是由于std::stringstream 的内部缓冲区rdbuf。
rdbuf, (type std::string_buf), 不支持non-copy 根据提案中的动机p0408r4:
...没有 非复制访问a的内部缓冲区
basic_stringbuf至少可以获取输出ostringstreaminefficient 的结果,因为 副本总是 制作
但是,已经有计划支持 std::string 在 stringsteam 的构造函数中移动:
explicit basic_ostringstream( basic_string<charT, traits, Allocator>&& str, ios_base::openmode which = ios_base::out, const Allocator& a = Allocator());
并移动str()
template<class SAlloc = Allocator> void str(basic_string<charT, traits, SAlloc>&& s);
【讨论】: