Why can't I pass an rvalue std::stringstream by value to a function?

隐身守侯 提交于 2019-12-07 09:46:14

问题


Why does this code not compile?

#include <sstream>

void f(std::stringstream) { }

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

I get this error:

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

If I replace std::stringstream with another type that's noncopyable it works fine. Shouldn't this use stringstream's move constructor?


回答1:


The missing move constructor for stringstream is a known missing feature in GCC's implementation of the C++ standard library. If I'm reading the comments on that report correctly, it is intentionally missing because it would rely on other changes, and those other changes would break the ABI, so the GCC folks have chosen to break the ABI only once (if possible), and that time has not yet come.



来源:https://stackoverflow.com/questions/20590449/why-cant-i-pass-an-rvalue-stdstringstream-by-value-to-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!