这是一个print()函数爱好者的福音

本文翻译自Readme.md


下载地址

点我直达


特性说明

  1. 易于阅读的彩色输出(当输出不是交互式终端时,颜色将自动禁用)
  2. 打印文件名,行号,函数名和原始表达式
  3. 打印类型信息
  4. 专门用于容器,指针,字符串文字,枚举和 std::optional 等的格式化输出。
  5. 可以在表达式内部使用
  6. 包含编译期间警告(防止忘记删掉这个库)
  7. 兼容 C++11, C++14, C++17 。

安装方法

  1. 直接复制 dbg.h 到系统/编译器的 include 目录。
  2. 如果不想随意更改include 目录,可以尝试安装 dbg-macro 软件包

额外说明

可以通过 #define DBG_MACRO_DISABLE 禁用 dbg()
可以通过 #define DBG_MACRO_NO_WARNING 禁用编译期间警告


示例代码

#include <vector>
#include <dbg.h>

// You can use "dbg(..)" in expressions:
int factorial(int n) {
  if (dbg(n <= 1)) {
    return dbg(1);
  } else {
    return dbg(n * factorial(n - 1));
  }
}

int main() {
  std::string message = "hello";
  dbg(message);  // [example.cpp:15 (main)] message = "hello" (std::string)

  const int a = 2;
  const int b = dbg(3 * a) + 1;  // [example.cpp:18 (main)] 3 * a = 6 (int)

  std::vector<int> numbers{b, 13, 42};
  dbg(numbers);  // [example.cpp:21 (main)] numbers = {7, 13, 42} (size: 3) (std::vector<int>)

  dbg("this line is executed");  // [example.cpp:23 (main)] this line is executed

  factorial(4);

  return 0;
}

更多说明

请查看项目地址


本站分流

V0.3.0

2020.01.06

  • Added dbg::type() helper to directly print type names

dbg-macro-0.3.0.tar.gz
[dbg-macro-0.3.0.zip](https://luxts-tuchuang.oss-cn-chengdu.aliyuncs.com/uploads/2020/02/dbg-macro-0.3.0.zip "dbg-macro-0.3.0.zip