简体   繁体   中英

why doesn't c++ uses RVO when returning local std::stringstream?

I've read lots of info about rvalue and returning local variables in C++ >= 11. From what I understood is that "just return by value, do not use move/forward and do not add && to method signature and the compiler will optimize it for you".

Okay, I want it happen:

#include <sstream>

std::stringstream GetStream() {
    std::stringstream s("test");
    return s;
}

auto main() -> int {
    auto s = GetStream();
}

I get the nice

error: use of deleted function ‘std::basic_stringstream<char>::basic_stringstream(const std::basic_stringstream<char>&)’
     return s;

error. I don't get it, why it tries the copy constructor? Shan't it use move constructor with all the nice things from c++11 here? I use "--std=c++14".

okay, this is a bug in my version of gcc (4.9.3). appears to be fixed in >= 5. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54316

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