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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:18:38+00:00 2026-06-17T10:18:38+00:00

I ran across this problem while cleaning up the debug macros of an old

  • 0

I ran across this problem while cleaning up the debug macros of an old C/C++ application: We have a Tracer class inheriting from ostrstream (I know it’s been deprecated since C++98, but this application was written in 1998!) which we use like this:

Tracer() << "some" << " message" << " here";

Now if the first value in the chain is a constant string like above, the result of calling ostrstream::str() on the Tracer (which is done in the destructor, inserting the result into a queue) contains a hexadecimal representation of the pointer to this string instead of the text. Thus the above statement would yield something like "0x401a37 message here". This didn’t occur with the old macros as they always had a long (Thread ID) as the first value which has now been removed.

Stepping into it with gdb showed that for the first insertion, this calls operator<<(void const*) on the ostrstream, while the subsequent insertions call operator<< <...>(basic_ostream<...>&, char const*) (templating removed for readability).

Can somebody explain this behaviour? What would be a clean way to fix this? I have found an easy workaround, which is using << left as the first argument – is this safe? Are there better ways to do this?

Here’s a minimized example:

#include <strstream>
#include <iostream>

using namespace std;

class Trace : public ostrstream {
    public:
        Trace();
        virtual ~Trace();
};

Trace::Trace() : ostrstream() {}
Trace::~Trace() {
    static_cast< ostrstream& >(*this) <<ends;
    char * text = ostrstream::str();
    cout << "MESSAGE: "<< text <<endl;
    delete[] text;
}

int main(){
    Trace() << "some" << " text" << " here";
    Trace() << left << "some" << " text" << " here";
    Trace() << 123 << " text" << " here";
}
  • 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-17T10:18:39+00:00Added an answer on June 17, 2026 at 10:18 am

    It works this way because the Tracer() is a temporary (rvalue) that can not bind to the non-const reference in operator<<(basic_ostream<...>&, .

    However, you can call member functions like operator<<(void const*), because that doesn’t require an lvalue.

    The member function then returns a reference to the stream object which can be used in calling the next operator<< in the sequence.

    Calling any member function this way, like Tracer() << left or Tracer() << flush, and thus “convert” the reference to an lvalue reference is quite safe.

    If you happen to have a C++11 compliant compiler, the standard library even contains an operator<<(basic_ostream<...>&&, which does this for you. In that case you don’t need the workaround anymore.

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

Sidebar

Related Questions

While just playing with jQuery/JavaScript I ran across this problem. I could capture Alt
I recently ran across this problem while trying to implement a service that has
While trying problem 41 from the Euler Project, I ran across what seems to
This one really has me stumped. I have not ran across this problem on
Hii , I am developing an application where I ran across into this problem
In answering this code golf question , I ran across a problem in my
I ran across this chunk of code (modified) in our application, and am confused
A situation I ran across this week: we have a jQuery Ajax call that
While perusing the source for AbstractMap in Java I ran across this: 440 /**
I ran across an odd problem while trying to transfer a project to 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.