Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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".

like image 439
Scott Tiger Avatar asked Mar 21 '16 13:03

Scott Tiger


1 Answers

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

like image 93
Scott Tiger Avatar answered Oct 24 '22 20:10

Scott Tiger