5

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

Scott Tiger
  • 478
  • 2
  • 12
  • 2
    This compile [without error](http://ideone.com/7aOvLQ). Which compiler are you using? – Holt Mar 21 '16 at 13:54
  • 4
    My crystal ball tells me you are using GCC 4.9, which doesn't support move semantics for `stringstream`. If you want to use C++14 then you should be using a compiler that supports it. – Jonathan Wakely Mar 21 '16 at 13:55
  • yeah, you are on the right way. gcc 4.9.3. – Scott Tiger Mar 21 '16 at 13:55
  • 1
    I wish I was as cognizant as @JonathanWakely – erip Mar 21 '16 at 13:56
  • 3
    @erip, there were three clues: I recognized the error message as one of GCC's; the first GCC version to support the "-std=c++14" option was 4.9.0; and I know I added move semantics to `stringstream` for GCC 5; so it must be GCC 4.9.x :) – Jonathan Wakely Mar 21 '16 at 14:00
  • `auto main() -> int` caused a little smile. :) – knivil Mar 21 '16 at 14:19
  • I just like how it looks, obviously I don't use it where there are code conventions, but what's wrong in having a little fun) – Scott Tiger Mar 21 '16 at 14:23

1 Answers1

9

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

Scott Tiger
  • 478
  • 2
  • 12