C ++中的C#样式枚举

我正在尝试编写一个使用外部工具的日志库

我正在寻找一种将键串添加到输出流的便捷方法,以帮助外部工具进行解析,同时对使用该库的程序员的影响最小

目标是实现以下目标:

cout << DEBUG::VERBOSE << "A should equal 3" << endl;
cout << DEBUG::WARNING << "something went wrong" << endl;


现在,我的数据结构如下

struct Debug
{
static const std::string FATAL_ERROR; 
static const std::string ERROR;
static const std::string WARNING;
static const std::string IMPORTANT;
static const std::string INFORMATION;
static const std::string VERBOSE;
static const std::string DEBUG;
};


这项工作找到,但我想从std::string类型添加一个抽象级别。

在Java / C#中,我可以使用enum来实现写行为,如何在C ++中优雅地实现这一点。


最佳答案:

我认为在C ++ iostream中,endl样式的流操纵器更加惯用:

#include <iostream>

namespace debug
{
    std::ostream & info(std::ostream & os) { return os << "Info: "; }
    std::ostream & warn(std::ostream & os) { return os << "Warning: "; }
    std::ostream & error(std::ostream & os) { return os << "Error: "; }
}

int main()
{
    std::cout << debug::info << "This is main()\n"
              << debug::error << "Everything is broken\n";
}