为{0,1}参数重载预处理器宏
Overloading Pre-processor macros for {zero, one} arguments
我在c++中为我的应用程序制作一个记录器类。该类具有静态成员,用于将调试输出记录到文件中。我想创建一个可以以两种方式使用的宏:
LOG("Log some text")
which calls Logger::log(std::string)
——或
LOG << "Log some text" << std::endl;
which calls Logger::getLogStream()
目标是包含Logger.h足以将日志记录到文件中,并且使用相同的语法完成,但如果您有其他建议,我不会特别依赖宏。不幸的是,不能使用Boost::PP。
我已经看了一下(见这个注释),但是没有发现任何关于区分LOG和LOG()调用的内容。我如何区分有论证和无论证?
可以重载Logger类的操作符,避免使用宏
class Logger
{
public:
void operator( )( const std::string& ar_text )
{
log( ar_text );
}
Logger& operator<<( const std::string& ar_text )
{
// use logStream here
return *this;
}
Logger& operator<<(std::ostream& (*f)(std::ostream&)) // This is necessary for grabbing std::endl;
{
// use logStream here
return *this;
}
};
相关文章:
- 错误:无效的预处理指令 #i 的意思是 #if?
- C++预处理会生成变量成员、资源库和映射
- 使用预处理指令检查是否包含标头?
- 预处理的 C/C++ 文件是否特定于计算机?
- 使用 GCC 对 C 文件进行部分预处理(不删除 "define" 指令)
- 在 CPLEX 中求解线性规划,无需剪切和预处理
- CPP -D 选项,用于预处理 Fortran 代码
- 错误:粘贴"tmp_UINT"和"+"未提供有效的预处理令牌
- 任务计划程序库的预处理不起作用 - 多定义错误
- Eclipse 问题 - 编译期间不考虑 .c 和 .cpp 文件中定义的预处理
- 使用python预处理后,C++(opencv)中的垫子类型数据与image_to_array相同
- Howo 使用 cl 预处理为 masm 组装生成一个单独的文件
- 我有一个预处理的 C/C++ 源文件 (cacti.i).如何从这个 .i 文件生成可执行二进制文件,以便我可以像 ./
- 如何使用Visual Studio C/C++编译器(cl.exe)来预处理我的objective-C代码
- 是具有预处理前分支实现的结构违反ODR
- 与不完整的Cholesky预处理的共轭梯度返回特征库的意外错误
- Visual Studio C - 无法输出预处理文件
- 在海湾合作委员会中加快宏观预处理的任何方法
- Xmllint未设置,而在路径中找不到XMLLINT;跳过XML预处理
- 如何在预处理时读取文件