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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:34:40+00:00 2026-05-25T16:34:40+00:00

Possible Duplicate: “Roll-Back” or Undo Any Manipulators Applied To A Stream Without Knowing What

  • 0

Possible Duplicate:
“Roll-Back” or Undo Any Manipulators Applied To A Stream Without Knowing What The Manipulators Were

Consider the following code

int temp=256;
cout<<temp<<endl;
cout<<hex<<temp<<endl;
cout<<temp<<endl;

The output is “256”,”100″ and “100” respectively.
Is it possible to make the ‘hex’ flag non-binding?

I do not wish to write ‘dec’ explicitly.

  • 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-25T16:34:41+00:00Added an answer on May 25, 2026 at 4:34 pm

    Not for the standard manipulators. But in practice, you probably
    shouldn’t be using the standard manipulators (other than perhaps as an
    example); they correspond to physical markup, and in an application, you
    want to use logical markup. You want to write something like

    cout << temperature << roomTemperature;
    

    , where temperature is an application specific manipulator, defining
    (globally) how temperatures are supposed to be output. That way, if the
    specification changes, and requires a different format for temperatures,
    you only have to change it in one place. Debugging output is somewhat
    of an exception here, but even there, it’s much easier to write
    something like:

    cout << HexFmt( 4 ) << var;
    

    than

    cout << hex << setfill( '0' ) << setw( 4 ) << var;
    

    (And you will probably use somthing like HexFmt often enough to
    justify having it in your toolbox.)

    Manipulators which you write yourself can be made to restore the
    previous state, at least at the end of the full expression. All of my
    manipulators derive from the following class:

    StateSavingManip.hh:

    class StateSavingManip : boost::noncopyable
    {
        mutable std::ios*   myStream;
        mutable std::ios::fmtflags
                            mySavedFlags;
        mutable int         mySavedPrec;
        mutable char        mySavedFill;
    
    private:
        virtual void        setState( std::ios& stream ) const = 0;
    
    protected:
        StateSavingManip();
    
    public:
        StateSavingManip( StateSavingManip const& other );
        virtual             ~StateSavingManip();
        void                operator()( std::ios& stream ) const;
    };
    
    inline std::ostream&
    operator<<(
        std::ostream&       out,
        StateSavingManip const&
                            manip )
    {
        manip( out );
        return out;
    }
    
    inline std::istream&
    operator>>(
        std::istream&       in,
        StateSavingManip const&
                            manip )
    {
        manip( in );
        return in;
    }
    

    StateSavingManip.cc:

    namespace {
    
        int                 getXAlloc();
        int                 ourXAlloc = getXAlloc() + 1;
    
        int
        getXAlloc()
        {
            if ( ourXAlloc == 0 ) {
                ourXAlloc = std::ios::xalloc() + 1;
                assert( ourXAlloc != 0 );
            }
            return ourXAlloc - 1;
        }
    }
    
    StateSavingManip::StateSavingManip()
        :   myStream( NULL )
    {
    }
    
    StateSavingManip::StateSavingManip(
        StateSavingManip const&
                            other )
    {
        assert( other.myStream == NULL );
    }
    
    StateSavingManip::~StateSavingManip()
    {
        if ( myStream != NULL ) {
            myStream->flags( mySavedFlags );
            myStream->precision( mySavedPrec );
            myStream->fill( mySavedFill );
            myStream->pword( getXAlloc() ) = NULL;
        }
    }
    
    void
    StateSavingManip::operator()( 
        std::ios&           stream ) const
    {
        void*&              backptr = stream.pword( getXAlloc() );
        if ( backptr == NULL ) {
            backptr      = const_cast< StateSavingManip* >( this );
            myStream     = &stream;
            mySavedFlags = stream.flags();
            mySavedPrec  = stream.precision();
            mySavedFill  = stream.fill();
        }
        setState( stream );
    }
    

    Which allows something simple like:

    class HexFmt : public StateSavingManip
    {
        int                 myWidth;
    protected:
        virtual void        setState( std::ios& targetStream ) const
        {
            targetStream.flags( std::ios::hex | std::ios::uppercase );
            targetStream.width( myWidth );
            targetStream.fill( '0' );
        }
    public:
        explicit            HexFmt( int width )
            : myWidth( width )
        {
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument Consider the following function:
Possible Duplicate: Weird “406 not acceptable” error following code generate a 406 Not Acceptable
Possible Duplicate: How to: Back button support “Ajax” I have a ASP.NET MVC implementation
Possible Duplicate: g++ “is not a type” error The following does not compile: 1
Possible Duplicate: mySQL “Rank in Highscore”-Query I was just wondering if there is any
Possible Duplicate: Undo “git add”? I made the mistake of running: git add .
Possible Duplicate: C++: “std::endl” vs “n” I'm wondering if there is any significant difference
Possible Duplicate: “Web interface” to PHPUnit tests? Are there any PHP unit testing systems
Possible Duplicate: Oracle “(+)” Operator So I was given a script with the following:
Possible Duplicate: Haskell “do nothing” IO, or if without else Something got wrong in

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.