这可以在宏中使用省略号吗? 可以转换成模板吗?
实现了 CLogClass 来进行体面的日志记录后,我还定义了宏,但它仅适用于一个参数...
class CLogClass
{
public:
static void DoLog(LPCTSTR sMessage, ...);
};
#define DebugLog(sMessage, x) ClogClass::DoLog(__FILE__, __LINE__, sMessage, x)
好吧,当使用超过 2 个参数调用时它会失败:(...是否有可能避免它?可以吗以某种方式转换为模板?
编辑: VS 2005 中引入了可变参数宏(但我目前在 VS 2003 中...)。
Having implemented CLogClass to make decent logging I also defined macro, but it works only with one parameter...
class CLogClass
{
public:
static void DoLog(LPCTSTR sMessage, ...);
};
#define DebugLog(sMessage, x) ClogClass::DoLog(__FILE__, __LINE__, sMessage, x)
Well, it fails when called with more than 2 parameters :( ... Is it possible at all to avoid it? Can it be translated to templates somehow?
EDIT: Variadic macros were introduced in VS 2005 (But i'm currently in VS 2003...). Any advices?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以让 MYLOG 宏返回一个自定义仿函数对象,该对象采用可变数量的参数。
请注意,过渡到 这个答案:由于
运算符<<
的链接效应,您不需要任何可变参数。You could have a MYLOG macro returning a custom functor object which takes a variable number of arguments.
Note that it's not so hard to step over to the type-safe variant touched by this answer: you don't need any variadic arguments due to the chaining effect of
operator<<
.你的问题实际上有两个答案。 您想要执行通用日志记录功能,其工作方式类似于 printf 但可以完全自定义。 因此,您需要:
以下是改编后的代码示例:
可变参数宏已在 C99 中引入,因此它将在支持 C99 或 C++0x 的编译器上工作。 我使用 gcc 3.4.2 和 Visual Studio 2005 成功测试了它。
函数的可变参数一直存在,所以这里不用担心兼容性。
可能可以通过一些模板元编程来做到这一点,但考虑到上面代码的简单性,我没有看到它的兴趣。
最后一点,为什么在空类中使用静态方法而不是函数?
Your questions actually appeals to two answers. You want to do the universal logging function, that works like printf but can be fully customise. So you need:
Here is your code example adatapted:
Variadic macros have been introduced in C99, so it will work on compilers supporting C99 or C++0x . I tested it successfully with gcc 3.4.2 and Visual Studio 2005.
Variadic arguments to functions have been there forever so no worry about compability here.
It's probably possible to do it with some template meta-programmaing but I don't see the interest of it given the simplicity of the code above.
As a last note, why use a static method in an empty class instead of a function ?
可以简单地扩展为写入时间戳、检查日志级别、写入文件等。
或者,更简单但不太灵活:
用法:
Can be trivially extended to write timestamps, check for loglevel, write to file, etc.
Or, more simply but less flexibly:
Usage:
在这种情况下,我倾向于使用全局可见的 extern 函数而不是宏,并使用 va_list 解析该函数中的省略号。 请参阅我之前的帖子,了解如何实现此目标的示例 。
I would tend to use a globally visible extern function rather than a macro in this instance, and resolve the ellipsis in this function using a va_list. See my previous post for an example on how to achieve this.