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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:14:17+00:00 2026-05-26T13:14:17+00:00

I would like to be able to do: foo(stringstream()<<number = << 500); EDIT: single

  • 0

I would like to be able to do:

foo(stringstream()<<"number = " << 500);

EDIT: single line solution is crucial since this is for logging purposes. These will be all around the code.

inside foo will print the string to screen or something of the sort.

now since stringstream’s operator<< returns ostream&, foo’s signature must be:

foo(ostream& o);

but how can I convert ostream& to string? (or char*).
Different approaches to achieving this use case are welcome as well.

  • 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-26T13:14:17+00:00Added an answer on May 26, 2026 at 1:14 pm

    The obvious solution is to use dynamic_cast in foo. But the given
    code still won’t work. (Your example will compile, but it won’t do what
    you think it should.) The expression std::ostringstream() is a
    temporary, you can’t initialize a non-const reference with a temporary,
    and the first argument of std::operator<<( std::ostream&, char const*)
    is a non-const reference. (You can call a member function on a
    temporary. Like std::ostream::operator<<( void const* ). So the code
    will compile, but it won’t do what you expect.

    You can work around this problem, using something like:

    foo( std::ostringstream().flush() << "number = " << 500 );
    

    std::ostream::flush() returns a non-const reference, so there are no
    further problems. And on a freshly created stream, it is a no-op.
    Still, I think you’ll agree that it isn’t the most elegant or intuitive
    solution.

    What I usually do in such cases is create a wrapper class, which
    contains it’s own std::ostringstream, and provides a templated
    member operator<< which forwards to the contained
    std::ostringstream. Your function foo would take a const
    reference to this—or what I offen do is have the destructor call
    foo directly, so that the client code doesn’t even have to worry about
    it; it does something like:

    log() << "number = " << 500;
    

    The function log() returns an instance of the wrapper class (but see
    below), and the (final) destructor of this class calls your function
    foo.

    There is one slight problem with this. The return value may be copied,
    and destructed immediately after the copy. Which will wreck havoc with
    what I just explained; in fact, since std::ostringstream isn’t
    copyable, it won’t even compile. The solution here is to put all of the
    actual logic, including the instance of std::ostringstream and the
    destructor logic calling foo in a separate implementation class, have
    the public wrapper have a boost::shared_ptr to it, and forward. Or
    just reimplement a bit of the shared pointer logic in your class:

    class LogWrapper
    {
        std::ostringstream* collector;
        int* useCount;
    public:
        LogWrapper()
            : collector(new std::ostringstream)
            , useCount(new int(1))
        {
        }
    
        ~LogWrapper()
        {
            -- *useCount;
            if ( *useCount == 0 ) {
                foo( collector->str() );
                delete collector;
                delete useCount;
            }
        }
    
        template<typename T>
        LogWrapper& operator<<( T const& value )
        {
            (*collector) << value;
            return *this;
        }
    };
    

    Note that it’s easy to extend this to support optional logging; just
    provide a constructor for the LogWrapper which sets collector to
    NULL, and test for this in the operator<<.

    EDITED:

    One other thing occurs to me: you’ll probably want to check whether the
    destructor is being called as a result of an exception, and not call
    foo in that case. Logically, I’d hope that the only exception you
    might get is std::bad_alloc, but there will always be a user who
    writes something like:

    log() << a + b;
    

    where the + is a user defined overload which throws.

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

Sidebar

Related Questions

I would like to be able to edit text in a UITextField like this:
I would like to be able to embed a command line interpreter inside a
I would like to be able to declare a function as void foo(<any value
I would like to be able to achieve something like this: class Zot {
In Python, you can something like this: >>> list(map(str.upper, ['foo','bar'])) ['FOO', 'BAR'] I would
Would like to be able to set colors of headings and such, different font
I would like to be able to loop through all of the defined parameters
We would like to be able to nightly make a copy/backup/snapshot of a production
I would like to be able to use the Tab key within a text
I would like to be able to display some dynamic text at the mouse

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.