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 9166771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:13:45+00:00 2026-06-17T15:13:45+00:00

Disclaimer: This question is for understanding. I’ll use boost::lexical_cast in the field. It has

  • 0

Disclaimer: This question is for understanding. I’ll use boost::lexical_cast in the field. It has sort of come up in the real world in places, though.


Take the following attempt at an “inline” lex-cast approach:

#include <string>
#include <sstream>
#include <iostream>

int main()
{
    const std::string s = static_cast<std::ostringstream&>(
       std::ostringstream() << "hi" << 0
    ).str();
    std::cout << s;
}

The result is something like 0x804947c0, because the operator<< that works with "hi" is a free function whose LHS must take std::ostream&†, and temporary std::ostringstream() can’t bind to a ref-to-non-const. The only remaining match is the operator<< that takes const void* on the RHS††.

Now let’s swap the operands:

#include <string>
#include <sstream>
#include <iostream>

int main()
{
    const std::string s = static_cast<std::ostringstream&>(
       std::ostringstream() << 0 << "hi"
    ).str();
    std::cout << s;
}

The result is "0hi".

This mostly makes sense, because the operator<< that takes int is a member function of base ostream††† and, as such, is fine with being invoked on the temporary. The result of that operation is a reference to the ostream base, to which the next operator<< is chained, i.e. read it as (std::ostringstream() << 0) << "hi".

But why then does that operation on "hi" go on to yield the expected result? Isn’t the reference on the LHS still a temporary?


Let’s focus on C++03; I’m told that the first example may actually work as “intended” in C++11 due to the catch-all operator for rvalues.

† [C++03: 27.6.2.1]: template<class charT, class traits>
basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&,charT*);

†† [C++03: 27.6.2.1]: basic_ostream<charT,traits>& operator<<(const void* p);

††† [C++03: 27.6.2.1]: basic_ostream<charT,traits>& operator<<(int n);

  • 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-06-17T15:13:46+00:00Added an answer on June 17, 2026 at 3:13 pm

    The reason is simple. If you read the question I asked about:

    std::ostringstream printing the address of the c-string instead of its content.

    you will note that the trick to getting a “proper” reference instead of a temporary is to call a method on the object (not restricted to the not binding restriction for some reason) that will return a reference.

    In Nawaz’s answer above, he called std::ostream& std::ostream::flush(), in your case here:

    std::ostringstream() << 0 << "hi"
    

    you call std::ostringstream& std::ostringstream::operator<<(int).

    Same result.

    The surprising behavior is due to ostream mishmash implementations: some operator<< are member methods while others are free-functions.

    You can test it, simply, by implementing an X& ref() method on an object:

    struct X { X& ref(); };
    
    void call(X& x);
    
    int main() {
        call(X{});       // error: cannot bind X& to a temporary
        call(X{}.ref()); // OK
    }
    

    EDIT: but why is not X& (the result of ref) treated the same ?

    It is a matter of classification. A temporary is a prvalue whilst a reference is a lvalue. A reference is only allowed to bind to a lvalue.

    Of course since methods can be called on rvalue (and thus prvalue) and those methods may return a reference to the objects they were called on we can easily bypass the silly (1) a reference is only allowed to bind to a lvalue restriction…

    (1) it’s also inconsistent with the fact that a rvalue can be bound to a const-reference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

DISCLAIMER: This is not a real-world example. It is just a theoretical question of
( DISCLAIMER : This is NOT a question about understanding the difference between abstract
Disclaimer: I know this type of question has been asked here before, I just
Disclaimer : This is a very basic question, but keep in mind I come
Disclaimer: this question is driven by my personal curiosity more than an actual need
Disclaimer: This question is strictly academic. The example I'm about to give is probably
Disclaimer: this is not a question about how to install asp.net or an application
Disclaimer: This question is not about fixing visual studio So, I've used VSS for
( Disclaimer: This question is not specific to ASP.NET) I have a control which
DISCLAIMER: This question was not meant to be argumentative! What is fastest and less

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.