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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:16:45+00:00 2026-05-10T20:16:45+00:00

Original Question I am writting a logging class where the goal is to be

  • 0

Original Question

I am writting a logging class where the goal is to be able to do this:

// thread one Logger() << 'Some string' << std::ios::hex << 45; // thread two Logger() << L'Some wide string' << std::endl; 

Currently my Logger header looks something like this:

#pragma once; #include <ostream>     class Logger { public:     Logger();     ~Logger();      std::ostream* out_stream; };  template <typename T> Logger& operator<< (Logger& logger, T thing) {     *logger.out_stream << thing;     return logger; } 

Some notes about this class:

  1. Cross platform compatibility is not an issue.
  2. Inside of Logger.cpp there is a singleton class that takes care of creating the ‘real’ ostream.
  3. The Logger constructor and deconstructor perform the necessary locking of the singleton.

I have three problems:

  • How do I make the operator<< function a friend or member so I can set out_stream as private?
  • How do I make the operator<< function work for manipulators?
  • How can I add a specialization so that if T is a WCHAR* or std::wstring that it will convert it to char* or std::string before passing it to out_stream? (I can do the conversion. Losing high unicode characters isn’t a problem in my case.)

Summary of things learned in answers:

  • Put template BEFORE friend instead of after.
  • std::ios::hex is not a manipulator. std::hex is a manipulator.

End Result

#pragma once #include <ostream> #include <string>  std::string ConvertWstringToString(std::wstring wstr);  class Logger { public:     Logger();     ~Logger();      template <typename T>     Logger& operator<< (T data) {         *out << data;         return *this;     }     Logger& operator<< (std::wstring data) {         return *this << ConvertWstringToString(data);     }     Logger& operator<< (const wchar_t* data) {         std::wstring str(data);         return *this << str;     }  private:     std::ostream* out; }; 
  • 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. 2026-05-10T20:16:46+00:00Added an answer on May 10, 2026 at 8:16 pm

    You can use friend definition, which will define the operator in the surrounding namespace of the class, and make it only visible to operator overloading resolution (not callable manually using the ::operator<<… syntax):

    class Logger { public:     Logger();     ~Logger();      std::ostream* out_stream;      template <typename T>     friend Logger& operator<< (Logger& logger, T thing) {         *logger.out_stream << thing;         return logger;     }      /* special treatment for std::wstring. just overload the operator! No need      * to specialize it. */     friend Logger& operator<< (Logger& logger, const std::wstring & wstr) {         /* do something here */     }  }; 

    The alternative, to keep your code as it is and just make the operator<< template a friend, you add this line into your class definition:

    template <typename T> friend Logger& operator<< (Logger& logger, T thing); 

    For the manipulator problem, i will just give you my code i write some time ago:

    #include <iostream> #include <cstdlib> using namespace std;  template<typename Char, typename Traits = char_traits<Char> > struct logger{     typedef std::basic_ostream<Char, Traits> ostream_type;     typedef ostream_type& (*manip_type)(ostream_type&);     logger(ostream_type& os):os(os){}     logger &operator<<(manip_type pfn) {         if(pfn == static_cast<manip_type>(std::endl)) {             time_t t = time(0);             os << ' --- ' << ctime(&t) << pfn;          } else             os << pfn;         return *this;      }     template<typename T>      logger &operator<<(T const& t) {          os << t;          return *this;     } private:             ostream_type & os; };  namespace { logger<char> clogged(cout); } int main() { clogged << 'something with log functionality' << std::endl; } 

    };

    Note that it is std::hex , but not std::ios::hex. The latter is used as a manipulator flag for the setf function of streams. Note that for your example, tho, no special treatment of manipulators is required. The above special treatment of std::endl is only needed because i do stream the time in addition when std::endl is used.

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

Sidebar

Related Questions

Original Question I want to be able to generate a new (fully valid) MP3
My original question can be found here , for which I've gotten some great
Edit: original question below, but I revise it now that I have some code
The original question I'm new to STM. One thing I'd like to do in
I know there is more than one question out there that matches this, but
Original Question I am looking for a function that attempts to quantify how distant
Original Question: i read that for RESTful websites. it is not good to use
Original question: The polysemy of a word is the number of senses it has.
(The original question was asked there : http://www.ogre3d.org/phpBB2/viewtopic.php?t=44832 ) Someone asked : While I
The original question is below, but I changed the title because I think it

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.