c++ - #define 命名空间中的语句

标签 c++ namespaces

如果我在这样的命名空间中有 #define 语句:

namespace MyNamespace
{
  #define SOME_VALUE 0xDEADBABE
}

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

以下是“正确”的做法吗?

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

最佳答案

正确,#define 不受命名空间的约束。 #definepreprocessor指令 - 它导致在通过编译器编译之前对源文件进行操作。命名空间在编译步骤中使用,编译器无法深入了解 #define 的内容。

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

关于c++ - #define 命名空间中的语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1088290/

相关文章:

clojure - 在当前 .jar 文件中获取 Clojure 命名空间

php - 无法在命名空间类中创建对象

c++ - 为什么 std::multimap 没有实现 try_emplace?

c++ - map 中具有指针类型的 Const Boost 变体

c++ - 使用点后模板函数的特化会破坏编译

c# - 是否可以沙盒化并运行在浏览器的文本字段中输入的 C++ 或 C# 代码?

php - 在层次结构目录结构中使用 PHP 命名空间错误

python - 在python中导入全局命名空间

c++ - 具有 Boost 格式的 int 特征向量的格式化 cout

c++ - 为什么 boost::bind 坚持将 `boost::placeholders` 拉入全局命名空间?