声明用于日志记录的类的模块名称
我目前正在向我们的日志库添加一些功能。其中之一是可以为一个类声明一个模块名称,该名称会自动添加到从该类中写入的任何日志消息之前。但是,如果未提供模块名称,则不会在前面添加任何内容。目前我正在使用一个具有返回名称的静态函数的特征类。
template< class T >
struct ModuleNameTrait {
static std::string Value() { return ""; }
};
template< >
struct ModuleNameTrait< Foo > {
static std::string Value() { return "Foo"; }
};
可以使用辅助宏来定义此类。缺点是,模块名称必须在类之外声明。我希望这在课堂上成为可能。另外,我希望能够使用预处理器指令删除所有日志记录代码。我知道使用 SFINAE 可以检查模板参数是否具有某个成员,但由于其他人对模板不像我那么友好,因此必须维护代码,所以我正在寻找一种更简单的解决方案。如果没有,我将坚持使用特质方法。
提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这对于您的方法来说是不可能的,必须在模板所属的命名空间中声明显式专业化。
您没有说明实际使用代码的样子,但您应该能够让名称和重载解析为您工作(例如,从日志记录宏):
如果您想定义
name()
仅在类中而不是在外部,当然不需要模板或重载: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:我不确定解决方案应该有多简单,这是我使用过几次的非常简单的解决方案。
有一个基类
ClassName
,其内容类似于:其他类将继承它,并给出其名称:
I'm not sure how simple the solution should be, here's a very simple one I used a few times.
There is a base class
ClassName
with something like:And other class would inherit it, giving its name: