Multi-line DEFINE directives?(多行 DEFINE 指令?)
问题描述
我不是专家,所以请放轻松.这两个代码段有什么区别吗?
I am not an expert, so go easy on me. Are there any differences between these two code segments?
#define BIT3 (0x1
<
<
3)
static int a;
和
#define BIT3 (0x1 << 3) static int a;
另外,有没有办法在一行中写第一个?这种多线样式的意义何在?下面的代码好吗?
Also, is there a way to write the first in one line? What is the point of this multi-line style? Is the following code good?
#define BIT3 (0x1 << 3)
static int a;
推荐答案
如果您有一个非常复杂的宏,如果它全部在一行上将难以阅读(尽管不建议使用多行宏)非常复杂的宏).
A multi-line macro is useful if you have a very complex macro which would be difficult to read if it were all on one line (although it's inadvisable to have very complex macros).
一般来说,您可以使用续行字符 编写多行定义.所以例如
In general, you can write a multi-line define using the line-continuation character, . So e.g.
#define MY_MACRO printf(
"I like %d types of cheese
",
5
)
但是你不能在你的第一个例子中做到这一点.你不能像那样拆分令牌;<<
左移运算符必须始终在没有任何分隔空格的情况下编写,否则它将被解释为两个小于运算符.所以也许:
But you cannot do that with your first example. You cannot split tokens like that; the <<
left-shift operator must always be written without any separating whitespace, otherwise it would be interpreted as two less-than operators. So maybe:
#define BIT3 (0x1
<<
3)
static int a;
现在相当于你的第二个例子.
which is now equivalent to your second example.
[虽然我不确定那个宏会有什么用处!]
这篇关于多行 DEFINE 指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:多行 DEFINE 指令?
- Stroustrup 的 Simple_window.h 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- C++ 协变模板 2021-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 近似搜索的工作原理 2021-01-01
- 静态初始化顺序失败 2022-01-01
- 从python回调到c++的选项 2022-11-16
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01