【问题标题】:Why can't I pass an rvalue std::stringstream by value to a function?为什么我不能将右值 std::stringstream 按值传递给函数?
【发布时间】:2013-12-15 02:03:44
【问题描述】:

为什么这段代码无法编译?

#include <sstream>

void f(std::stringstream) { }

int main() {
    f(std::stringstream{});
}

我收到此错误:

error: use of deleted function ‘std::basic_stringstream<char>::basic_stringstream(const std::basic_stringstream<char>&)’
         f(std::stringstream{});
                              ^

如果我将std::stringstream 替换为另一种不可复制的类型,它就可以正常工作。这不应该使用stringstream的移动构造函数吗?

【问题讨论】:

  • 在 MacOS 上使用 clang-500.2.79 和 -std=c++11 代码编译。您使用的是哪个编译器和标准库?
  • 您的标准库实现可能不完整。
  • @razeh 我在 debian 测试中使用 GCC 4.8.2 和 clang 4.2.1,不知道如何判断我使用的是什么 stdlib。
  • @BenjaminLindley 我在 debian 测试中使用 GCC 4.8.2 和 clang 4.2.1 与它捆绑的 stdlib,看起来应该是最新的。
  • @uk4321:也许应该是,但不是。尝试一个简单的测试:std::stringstream s1; std::stringstream s2(std::move(s1));

标签: c++ c++11 move-semantics rvalue


【解决方案1】:

在 GCC 的 C++ 标准库实现中,stringstream 缺少的移动构造函数是 a known missing feature。如果我正确阅读了该报告中的 cmets,那么它是故意丢失的,因为它会依赖于其他更改,而这些其他更改会破坏 ABI,因此 GCC 人员选择只破坏 ABI 一次(如果可能),而那个时候还没有到来。

【讨论】:

    猜你喜欢
    • 2018-01-20
    • 1970-01-01
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 2018-09-03
    相关资源
    最近更新 更多