简体   繁体   中英

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM