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!
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):
If you want to define
name()only in classes and not outside, there is of course no need for templates or overloads: