C++ #define 命名空间中的语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1088290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:45:07  来源:igfitidea点击:

#define statements within a namespace

c++namespaces

提问by Soo Wei Tan

If I have a #define statement within a namespace as such:

如果我在命名空间中有一个 #define 语句:

namespace MyNamespace
{
  #define SOME_VALUE 0xDEADBABE
}

Am I correct in saying that the #define statement is not restricted to the namespace?

我说#define 语句不限于命名空间是否正确?

Is the following the "correct" thing to do?

以下是“正确”的事情吗?

namespace MyNamespace
{
  const unsigned int SOME_VALUE = 0xDEADBABE;
}

回答by Michael

Correct,#define's aren't bound by namespaces. #defineis a preprocessordirective - it results in manipulation of the source file prior to being compiled via the compiler. Namespaces are used during the compilation step and the compiler has no insight into the #define's.

正确,#define's 不受命名空间的约束。 #define是一个预处理器指令 - 它导致在通过编译器编译之前对源文件进行操作。命名空间在编译步骤中使用,编译器无法洞察#define's。

You should try to avoid the preprocessor as much as possible. For constant values like this, prefer const over #define's.

您应该尽量避免使用预处理器。对于像这样的常量值,更喜欢 const 而不是#define's。

回答by nik

I completely agree with the suggestions on the use of constants and the scope being unlimited for #defines.

我完全同意有关使用常量的建议以及#defines的范围不受限制。

However, if you dohave to use preprocessor #definelines, please cover them up correctly for the expected scope,

但是,如果您确实必须使用预处理器#define行,请在预期范围内正确覆盖它们,

namespace MyNamespace
{
  #define SOME_VALUE 0xDEADBABE
  // your code
  #undef SOME_VALUE
}

Why #defines?
I know one case where an embedded platform did not support constants in the code.
There was no way to initialize them...
It always helps to be more readable.

为什么#defines
我知道一种嵌入式平台不支持代码中的常量的情况。
没有办法初始化它们......
它总是有助于提高可读性。

回答by Alex Martelli

The preprocessor operates (conceptually at least, and often actually too) before namespaces and other language-level concepts "kick in" -- so yes, it's definitelymuchbetter to use language-level constructs such as constvalues wherever you can!

预处理器在命名空间和其他语言级概念“启动”之前运行(至少在概念上,通常也是如此) - 所以是的,在任何地方使用语言级构造(例如值)肯定好得多const

回答by Reed Copsey

Yes. In general, using const values instead of #define'd values has many advantages. Restricting the scope of the variable is one of the advantages. The scope can be restricted to a namespace, or to any another valid scope this way (including at the class level, function level, etc).

是的。一般来说,使用 const 值而不是 #define 的值有很多优点。限制变量的范围是优点之一。可以通过这种方式将范围限制为命名空间或任何其他有效范围(包括在类级别、函数级别等)。

回答by Mark Rushakoff

If, for some reason, you couldn't change to the second case, I'm pretty sure you would have to take care to compile MyNamespace into its own object and link the objects separately (or possibly just run the preprocessor on that single namespace by itself). The C++ preprocessor should take that #definestatement and essentially do a string replace anywherethat SOME_VALUEis seen in the source code. Hence if the preprocessor is not aware of the #define, SOME_VALUEcannot be replaced in another source file.

如果由于某种原因,您无法更改为第二种情况,我很确定您必须小心地将 MyNamespace 编译成它自己的对象并单独链接这些对象(或者可能只是在该单个命名空间上运行预处理器通过它自己)。C ++预处理器应该采取的#define声明,基本上做一个字符串替换任何地方SOME_VALUE是在源代码中看到。因此,如果预处理器不知道#define,SOME_VALUE则无法在另一个源文件中替换。