Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7037039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:29:44+00:00 2026-05-28T01:29:44+00:00

§27.7.3.9 defines the following overload for operator<< : template <class charT, class traits, class

  • 0

§27.7.3.9 defines the following overload for operator<<:

template <class charT, class traits, class T>
  basic_ostream<charT, traits>&
  operator<<(basic_ostream<charT, traits>&& os, const T& x);

Effects: os << x
Returns: os

(§27.7.2.6 defines the rvalue overload for operator>>.)
Basically, it just forwards to an lvalue overload. I consider this overload to be pretty dangerous (the istream one even more so than the ostream one, actually), consider the following:

#include <sstream>
#include <iostream>

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

Live example on Ideone (perfect example of undefined behaviour. Prints nothing for me on MSVC10).

The above example might look contrived, but it shouldn’t be too hard to get into this situation in generic code or when passing the (std::stringstream() << "text") to a function that provides an lvalue and an rvalue overload and stores the std::ostream or std::istream in different ways according to the overload.

Now, what would be an argument agains returning a basic_ostream<charT, traits>&& and specifying the following?

Returns: move(os)

(And the same for basic_istream.)

Is there anything I’m overlooking? At the current state, in my eyes, it just looks dangerous and like a defect. I skimmed through the LWG issue list and found this proposal (hi @HowardHinnant!). It indeed returns an rvalue, however only for the added benefit of being able to chain this special operator, not specifically addressing the safety issue I described above (though it certainly does solve it). Additionally, it’s marked as closed and for reconsideration for the next standard. As such, I thought I’d ask here:

Is there any good reason why the above mentioned overload returns an lvalue reference?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:29:45+00:00Added an answer on May 28, 2026 at 1:29 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created UITableCellView class called NoteCell . The header defines the following: #import
I've made a program for my OOP class that does the following: Defines a
I am trying to overload the << operator for my Currency class but I
I have a class which defines the following members in a header file (.hpp):
What is a recommended way to overload the output stream operator? The following can
I'm looking at a batch file which defines the following variables: set _SCRIPT_DRIVE=%~d0 set
The following XAML (below) defines a custom collection in resources and attempts to populate
Consider the following code // BOGP.cpp : Defines the entry point for the console
When refactoring away some #defines I came across declarations similar to the following in
According to the sqlite3 documentation , The COLLATE clause following each column name defines

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.