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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:40:02+00:00 2026-05-30T21:40:02+00:00

I have a totally basic C++ question here. #include <iostream> using namespace std; int

  • 0

I have a totally basic C++ question here.

#include <iostream>
using namespace std;

int main() {
    int a = 255;
    cout << hex << a << endl; // <-----
}

In the code piece above, how is the std::cout statement chained?

I understand that an implementation of cout would return the reference to cout object to allow chaining to happen, so it should be executed as:

(((cout << hex) << a) << endl)

i.e. equivalent to these, in order

  1. cout << hex
  2. cout << a
  3. cout << endl

But this cannot be the case because somehow value of a needs to be converted to hex form!

How are operators actually chained by the compiler to make the conversion happen?

  • 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-30T21:40:04+00:00Added an answer on May 30, 2026 at 9:40 pm

    Here is how hex is usually implemented:

    inline ios_base&
    hex(ios_base& __base)
    {
        __base.setf(ios_base::hex, ios_base::basefield);
        return __base;
    }
    

    As you can see, hex does not perform any conversion by itself: instead, it sets an option in the base stream to use hex for printing of numbers passed into it at a later point.

    EDIT (in response to a comment)

    As hammar correctly notes, the other part of the puzzle is how hex(ios_base& __base) is being called. There is an overload of the << operator with this signature:

    ostream& operator <<(ostream& (*)(ostream&))
    

    This overload is essential implementation detail of stream manipulators. It is this overload that calls hex, and lets it do its “magic” (which of course should not sound like magic to you any longer).

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

Sidebar

Related Questions

Totally confused here. I have a PARENT UIViewController that needs to pass an NSMutableArray
I'm a little new to asp.net mvc and I have a question (very basic).
This question must be really basic, but if you have any pointers I would
I'm new to Clojure and have been using Compojure to write a basic web
i have been using the basic toolkit that is provided with VS 2008 I
Many applications have dialogs which have totally custom-looking dialogs, big fat tool-bars and non-rectangular
I have a NSIS installer I want to be totally silent unless it needs
I am totally new to SQL . I have a simple select query similar
I have to write a reliable, totally-ordered multicast system from scratch in Python. I
Totally confused by this one... We have a WAMPServer installation set up, running 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.