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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:47:35+00:00 2026-06-06T15:47:35+00:00

From this question & the answers – What is the correct answer for cout

  • 0

From this question & the answers – What is the correct answer for cout << c++ << c;?

I get that

std::cout<<c++<<c;

is evaluated as:

std::operator<<(std::operator<<(std::cout, c++), c);

so the undefined behavior comes from the fact that either of the two parameters could be evaluated first. So far so good.

But why std::operator <<? Why isn’t std::ostream::operator << called? And if it is, wouldn’t it translate to

(ofstream::operator<<(c++)) << c;
              |
     returns ofstream&

What’s the difference between this and method chaining:

struct A
{
    A& foo();
    void goo();
};
//...
A a;
a.foo().goo();

?

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

    std::ostream provides operator<< as overloaded member operators, but other headers (e.g. <string>) provide free operators; so whether << is a member operator or a free function depends on the RHS type.

    However, it doesn’t matter either way. Let’s rename << as foo and cout as bar:

    foo(foo(bar, c++), c);
    bar.foo(c++).foo(c);
    

    In both cases behaviour is undefined because there is no requirement on the implementation to evaluate the arguments to either call to foo in any particular order. The important consideration is that per Annex C, a chained method call does not constitute more than one full-expression; if the compiler sees

    foo.bar(<some-complex-expression>).baz(<another-complex-expression>);
    

    it is free to apply CSE and reordering to the arguments to bar and to baz; indeed examination of side effects may show that the arguments to baz are evaluated before those to bar.

    struct A { A &foo(int) { return *this; } };
    #include <cstdio>
    int main() { A().foo(printf("hello\n")).foo(printf("bye\n")); }
    

    My compiler (gcc 4.1.2) generates a program which prints bye\nhello\n.

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

Sidebar

Related Questions

I'm gleaning from this question [ Facebook Connect Won't Validate that using Facebook Connect
There's probably a Google search that'll answer this question but for the life of
This question extensions from one of the answers to my earlier question: how to
Yes, I've read this question & answers: Passing an array by reference in C?
This question seems to get asked a lot, but I can't find an answer
From this question I learned Double.NaN is not equal to itself. I was verifying
Continuing from this question : When I am trying to do fopen on Windows,
This question is a follow on from this one ... I am binding to
Following on from this question , I am interested in finding out how you
following on from this question (Developing to an interface with TDD), I'm still having

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.