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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:39:20+00:00 2026-06-04T02:39:20+00:00

I have the following class: namespace { class MimeLogger : public std::ostringstream { public:

  • 0

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 "Hello " string is treated as a void*. If I debug the code, "Hello " is passed into std::basic_ostream::operator<< (void const*) and prints as a pointer value, not a string. The second string, "World" is properly passed into the global overloaded << operator that takes a char const*.

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

Thanks in advance.

Update

I neglected to mention that I’m stuck with C++03, but I’m glad that some people covered both the C++03 and C++11 cases.

  • 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-04T02:39:22+00:00Added an answer on June 4, 2026 at 2:39 am

    C++03: For the expression MimeLogger() << "Hello ", the template function

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

    is not considered during overload resolution because the temporary MimeLogger() may not be bound to a non-const reference. The member function overloads do not have this problem because the rules for the implicit parameter do allow binding to a temporary.

    If you can use a compiler with support for C++11 rvalue-references, this should work as you intended, because the C++11 library provides an additional overload

    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; }
    

    which allows temporary streams to be used left of << as though they were not temporary.

    (I did try a test program with g++ and got different results without and with -std=c++0x.)

    If you cannot use a C++11 friendly compiler, adding this to the public section of class MimeLogger is a workaround that will do what you want with C++03:

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

    The using-declaration makes sure the member overloads from the standard library are also visible from MimeLogger. In particular, without it manipulators like std::endl don’t work with the template operator, since std::endl is itself a function template, and that’s too much template type deduction to expect from C++. But things are fine as long as we’re sure not to hide the ostream member that makes the function manipulators work (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>&));
        };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following class [DataContract(Namespace = , Name = VersionRange)] public sealed class
I have the following class #ifndef Container_H #define Container_H #include <iostream> using namespace std;
Hello I have the following code namespace ConsoleApplication2 { class Program { static void
I have the following types and classes: namespace MVC.Models public class Page { public
I have the following code compiled by gcc: #include <iostream> using namespace std; class
If I have the following basic C++ program: #include <iostream> using namespace std; class
I have the following class: public class AddCouponInfoRequest : namespace.Request { } I have
I have following code: #include <iostream> using namespace std; template<class T> T max(T *data,int
i have following code #include <iostream> #include <utility> using namespace std; namespace rel_ops{ template<class
I have the following class: namespace Controls { public class DropDownButton : Control {

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.