Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The first string argument to a stringstream is saved as a pointer/garbage [duplicate]

Possible Duplicate:
Printing a string to a temporary stream object in C++
std::ostringstream printing the address of the c-string instead of its content

I'm trying to build up a string using stringstream, in the same way you'd use cout. This for something like a logging class. The issue I'm having is that if the first argument to the << operator is a string, when I subsequently print out that stringstream with the stringstream::str() call, I get a garbage address, and not the string. This only occurs with the FIRST string. Subsequent strings are fine. Numbers are always fine. Here's the code:

    // class I use to print out the stream
    class StreamWriter
    {
    public:
        StreamWriter()
        {}

        ~StreamWriter()
        {
            std::string myMessage = m_stringstream.str();
            std::cout << myMessage << std::endl;
        }

        std::stringstream m_stringstream;
    };

    // macro for simplification
    #define OSRDEBUG (StreamWriter().m_stringstream)

    // actual use
    OSRDEBUG << "Hello " << "my " << "name is Pris " << 123456;

    // output
    0x8054480my name is Pris 123456
    0x8054480my name is Pris 123456
    0x8054480my name is Pris 123456
    0x8054480my name is Pris 123456

Could anyone shed some light on what's going on, and how I could get around the issue?

EDIT: The following changes (in addition to padiablo's examples) works as well, maintaining the use of the class's destructor with the macro.

    // class I use to print out the stream
    class StreamWriter
    {
    public:
        StreamWriter()
        {   m_stringstream = new std::stringstream;   }

        ~StreamWriter()
        {
            std::string myMessage = m_stringstream.str();
            std::cout << myMessage << std::endl;
            delete m_stringstream;
        }

        std::stringstream * m_stringstream;
    };

    // macro for simplication
    #define OSRDEBUG *(StreamWriter().m_stringstream)

The original question still stands though, because it looks like it should work... and I think it's probably important to know when the times comes to put this into production-quality code.

like image 434
Prismatic Avatar asked Jan 17 '23 06:01

Prismatic


2 Answers

The problem is indeed that the stream is a temporary.

Before C++11, there was no non-member operator<< overload that took an rvalue reference as the first parameter (aka, allowing writes to temporary streams).

As such, the only valid operator<< overloads were the members, since all non-member overloads take a non-const reference and as such will not bind to temporaries. One of those non-member overloads is the one responsible for printing C strings (aka char const*). Here's one of the member overloads:

basic_ostream<Ch, Traits>& operator<<(void* p);

And guess what your string literal liberally converts to. :)

After the first string, you get a normal reference back from the call to operator<<, which will then allow the non-member overloads to be viable.

like image 172
Xeo Avatar answered Feb 02 '23 00:02

Xeo


I honestly don't understand exactly what's going on (it has something to do with your StreamWriter instance being a temporary), but I see the same effect as paxdiablo described in both GCC and MSVC.

However, here's something that can work around the problem. Add the following helper to your StreamWriter class:

    ostream& get_ostream() {
        return m_stringstream;
    }

and change the macro to:

#define OSRDEBUG (StreamWriter().get_ostream())
like image 38
Michael Burr Avatar answered Feb 02 '23 00:02

Michael Burr