Std :: ostringstream operator overload search order?

I have the following class:

namespace {
class MimeLogger : public std::ostringstream
{
public:
    MimeLogger()
    {}

    ~MimeLogger()
    {
        LOGEVENT( logModuleWSE, logEventDebug, logMsgWSETrace1, str() );
    }
};
}

When I do this:

MimeLogger() << "Hello " << "World";

The first line is "Hello "considered as void*. If I debug the code, it "Hello "gets passed to std::basic_ostream::operator<< (void const*)and prints as a pointer value, not a string. The second line is "World"properly passed to the global overloaded <statement that takes a char const*.

I expect both uses of the <operator to solve the same overload, but this does not happen. Can someone explain and maybe suggest a fix?

Thanks in advance.

Update

I forgot to mention that I was stuck with C ++ 03, but I'm glad that some people considered both C ++ 03 and C ++ 11 cases.

+5
source share
2

++ 03: MimeLogger() << "Hello "

template <typename charT, class traits>
std::basic_ostream<charT, traits>& std::operator<< (
    std::basic_ostream<charT, traits>& os,
    const char* cstr);

, MimeLogger() . - , .

++ 11 rvalue-, , , ++ 11

template <typename charT, class traits, typename T>
std::basic_ostream<charT, traits>& std::operator<< (
    std::basic_ostream<charT, traits>&& os,
    const T& x ); // { os << x; return os; }

<<, .

( g++ -std = ++ 0x.)

++ 11, class MimeLogger , , , ++ 03:

template<typename T>
MimeLogger& operator<<(const T& x)
{
    static_cast<std::ostringstream&>(*this) << x;
    return *this;
}

using std::ostringstream::operator<<;

- , , MimeLogger. , std::endl , std::endl ++. , ostream, (27.7.3.6.3):

namespace std {
    template <typename charT, class traits>
    class basic_ostream : /*...*/ {
    public:
        basic_ostream<charT, traits>& operator<<(
            basic_ostream<charT,traits>& (*pf)(basic_ostream<charT,traits>&));
    };
}
+7

std::ostringstream?

class MimeLogger
{
private:
    std::ostringstream oss_m;

public:
    MimeLogger()
    {
    }

    ~MimeLogger()
    {
        std::cout << __FILE__ << "(" << __LINE__ << "):" << oss_m.str() << "\n";
    }

    template<typename Type>
    MimeLogger& operator<<(const Type& t)
    {
        oss_m << t;
        return *this;
    }
};


void LogDemo()
{       
   MimeLogger logger;
   logger << "Hello " << "World!!\n";
   MimeLogger() << "Hello " << "StackOverflow!!\n";
}
0

All Articles