C++ - 宏

C++ 学习笔记

C++ 中的宏

预处理 - preprocessor

  • 当我们尝试去编译一个 C++ 程序的时候,第一件事情就是预处理
  • 预处理 可以理解成一个代码文本修改的过程,我们可以预先写好一些宏,用来替换一些代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string>
#include <iostream>

// 如果是 DEBUG_MODE,就打印,否则不打印日志
#ifdef DEBUG_MODE
#define LOG(x) std::cout << x << std::endl
#else
#define LOG(x)
#endif

// 程序的主函数
int main()
{
LOG("hello");
return 0;
}