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

  • Home
  • SEARCH
  • 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 681121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:26:54+00:00 2026-05-14T01:26:54+00:00

I currently am adding some features to our logging-library. One of these is the

  • 0

I currently am adding some features to our logging-library. One of these is the possibility to declare a module-name for a class that automatically gets preprended to any log-messages writing from within that class. However, if no module-name is provided, nothing is prepended. Currently I am using a trait-class that has a static function that returns the name.

template< class T >
struct ModuleNameTrait {
    static std::string Value() { return ""; }
};

template< >
struct ModuleNameTrait< Foo > {
    static std::string Value() { return "Foo"; }
};

This class can be defined using a helper-macro. The drawback is, that the module-name has to be declared outside of the class. I would like this to be possible within the class. Also, I want to be able to remove all logging-code using a preprocessor directive. I know that using SFINAE one can check if a template argument has a certain member, but since other people, that are not as friendly with templates as I am, will have to maintain the code, I am looking for a much simpler solution. If there is none, I will stick with the traits approach.

Thanks in advance!

  • 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-14T01:26:55+00:00Added an answer on May 14, 2026 at 1:26 am

    I would like this to be possible within the class.

    This is not possible with your approach, explicit specializations have to be declared in the namespace of which the template is a member.

    You don’t say how the actual using code looks like, but you should be able to let name and overload resolution work for you (e.g. from a logging macro):

    template<class T> const char* const name(const T&) { return ""; }
    
    class X;
    const char* const name(const X&) { return "X"; }
    
    struct X {
        // prints "X"
        void f() { std::cout << name(*this) <<  std::endl; }
    };
    
    struct Y {
        static const char* const name(const Y&) { return "Y"; }    
        // prints "Y"
        void f() { std::cout << name(*this) << std::endl; }
    };
    
    struct Z {
        // prints ""
        void f() { std::cout << name(*this) << std::endl; }
    };
    

    If you want to define name() only in classes and not outside, there is of course no need for templates or overloads:

    const char* const name() { return ""; }
    
    struct X {
        static const char* const name() { return "X"; }    
        // prints "X"
        void f() { std::cout << name() << std::endl; }
    };
    
    struct Y {
        // prints ""
        void f() { std::cout << name() <<  std::endl; }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm adding some javascript validation to a form. The form has fields that currently
I'm adding a toolbar to my application and currently I'm adding some toggle buttons
Currently I have a loop that tries to find an unused filename by adding
Background: We are developing a web application that includes charts. Currently one can left-click
I am currently adding features to a PHP web application for a client. They
We are currently working on a small project, we are updating and adding some
I'm currently adding some photo-taking functionality to my application. I'm curious about something, though.
I have ported our project from Delphi 7 to Delphi 2010. After adding some
I am currently doing some work for a company that runs a legacy web
I'm currently designing an app for the iPhone and I'm deciding on some features

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.