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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:40:01+00:00 2026-05-28T02:40:01+00:00

I have a class for logging, which MUST NOT inherit std::ostream, and has operator<<

  • 0

I have a class for logging, which MUST NOT inherit std::ostream, and has operator<< defined for the same types as the standard output stream PLUS a templated version:

class MyLoggingClass {
[...]
public:
    template<typename T> MyLoggingClass & operator<<( T& data ){ ... }
}

Also, for every printable class in my program, I have the typical non-member function defined:

std::ostream & operator << ( std::ostream & os, const OneOfMyClasses & foo );

The thing is that, internally, my logger sometimes uses an standard output stream, this is:

template<typename T>
MyLoggingClass & operator<<( T& data )
{
    [...]
    if( someCondition )
    {
        cout << data;
    }
    [...]
}

Thanks to this, I can log my classes without explicitly replicating on every of them the non-member operator<< for MyLoggingClass.
The problem comes in a line where I try to log an object created “on the fly”:

MyLoggingClass logger;
logger << OneOfMyClasses(params); // Here I am invoking the constructor of class "OneOfMyClasses"

The thing is that, instead of invoking the constructor, and then passing the object as parameter of the operator<<, it interprets that I’m trying to log a pointer to function.

Of course, some valid solutions for this problem include:

  • Removing the templated operator<<, and making MyLoggingClass extend std::ostream
  • Removing the templated operator<<, and creating tons of non-member operator<<( MyLoggingClass &, const OneOfMyClasses & )
  • Storing the object to log in a temporal variable, and then doing “logger << temporalObject;”
    However, I would like to know if there is a way to force the compiler evaluating the constructor call. Do you know any workaround for this case?

Thank you in advance for your time 🙂

  • 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-28T02:40:01+00:00Added an answer on May 28, 2026 at 2:40 am

    I think this is the problem is:

    template<typename T> MyLoggingClass & operator<<( T& data ){ ... }
    

    data is non-const reference and the line:

    logger << OneOfMyClasses(params);
    

    is attempting to bind a temporary to a non-const reference.

    Change to:

    template<typename T> MyLoggingClass & operator<<(const T& data ){ ... }
                                                   //^^^^^
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a logging class which has operator<< overloaded. So I can do things
I have a class (for logging) which derives from std::ostream. Stripped down, it looks
I have a static object defined in my logging class, along the lines of:
I'm looking at making a logging class which has members like Info, Error etc
I have a Log class that has several static methods which help log information
I have an ASP.NET MVC app which has a bootstrapper class that configures log4net
I have an instance of a logging class logger, this class has a function
At the company I work for, I have created a Error Logging class to
I am writing a C# application. I have (a kind of) logging class. and
I have written a class that will handle internal logging in my application. Now

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.