Why does the rvalue overload of `operator<<` for `basic_ostream` return an lvalue reference?

前端 未结 1 389
走了就别回头了
走了就别回头了 2020-12-18 18:00

§27.7.3.9 defines the following overload for operator<<:

template 
  basic_ostream&         


        
相关标签:
1条回答
  • 2020-12-18 18:52

    It's a defect and it is my fault, sorry. LWG 1203 (thanks for finding that for me! :-)) is my current opinion of the correct fix for the "rvalue-stream-inserter". Note though that you could still catch it and be in trouble:

    auto&& s = (std::stringstream() << "hi there!\n");
    std::cout << s.rdbuf(); // oops
    

    Though at least in the above code it is a little more obvious (because of the &&) that you're doing something you shouldn't.

    0 讨论(0)
提交回复
热议问题