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

  • Home
  • SEARCH
  • 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 7087745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:42:42+00:00 2026-05-28T07:42:42+00:00

I have a function that takes an ostream reference as an argument, writes some

  • 0

I have a function that takes an ostream reference as an argument, writes some data to the stream, and then returns a reference to that same stream, like so:

#include <iostream>

std::ostream& print( std::ostream& os ) {
  os << " How are you?" << std::endl;
  return os;
}

int main() {
  std::cout << "Hello, world!" << print( std::cout ) << std::endl;
}

The output of this code is:

 How are you?
Hello, world!0x601288

However, if I separate the chaining expressions into two statements, like this

int main() {
  std::cout << "Hello, world!";
  std::cout << print( std::cout ) << std::endl;
}

then I at least get the proper order in the output, but still get a hex value:

Hello, world! How are you?
0x600ec8

I would like to understand what’s going on here. Does a normal function take precedence over operator<<, and that’s why the output order reverses? What is the proper way to write a function that inserts data into an ostream but that can also chain with operator<<?

  • 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-28T07:42:43+00:00Added an answer on May 28, 2026 at 7:42 am

    The behavior of your code is unspecified as per the C++ Standard.

    Explanation

    The following (I removed std::endl for simplicity)

    std::cout << "Hello, world!" << print( std::cout ); 
    

    is equivalent to this:

    operator<<(operator<<(std::cout, "Hello, World!"), print(std::cout));
    

    which is a function call, passing two arguments:

    • First argument is : operator<<(std::cout, "Hello, World!")
    • Second argument is : print(std::cout)

    Now, the Standard doesn’t specify the order in which arguments are evaluated. It is unspecified. But your compiler seems to evaluate the second argument first, that is why it prints "How are you?" first, evaluating the second argument to a value of type std::ostream& which then gets passed to the call shown above (that value is the object std::cout itself).

    Why hexadecimal output?

    You get hexadecimal output because the second argument evaluates to std::cout, which is being printed as hexadecimal number, because std::cout implicitly converts into pointer value of void* type, which is why it is printed as hexadecimal number.

    Try this:

    void const *pointer = std::cout; //implicitly converts into pointer type!
    std::cout << std::cout << std::endl;
    std::cout << pointer << std::endl;
    

    It will print the same value for both. For example, this example at ideone prints this:

    0x804a044
    0x804a044 
    

    Also note that I didn’t use explicit cast; rather std::cout is implicitly converted into pointer type.

    Hope that helps.


    What is the proper way to write a function that inserts data into an ostream but that can also chain with operator<<?

    When it depends on what you mean by chaining? Obviously, the following wouldn’t work (as explained above):

    std::cout << X << print(std::cout) << Y << Z; //unspecified behaviour!
    

    No matter how you write print().

    However this is well-defined:

    print(std::cout) << X << Y << Z; //well-defined behaviour!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that takes some bitmap data and returns a UIImage *
I have a function that takes a string-like argument. I want to decide if
I have a function that takes a pointer as a reference argument, but I
I have a function that takes an opened file object file and writes data
I have a function that takes a list of floats and returns the same
I have a function that takes the exact same args, but sometimes I'd like
I have a Python function that takes a numeric argument that must be an
I have a function that takes an array as a parameter. The function then
I have a function that takes a list and replaces some elements. I have
I have a function that takes a callback argument. This can be either a

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.