#undef C++ 的范围

发布于 2024-09-30 05:57:45 字数 557 浏览 3 评论 0原文


我对使用 #undef 重新定义宏有疑问。
我有一个文件global.h,其中包含许多#define-d 宏。在使用这些宏的代码中,我发现宏所保存的值不够通用。我想重新定义宏以使它们更通用。我编写了以下代码片段来做到这一点:

 std::cout << endl << "Enter pitch threshold:" << endl;  
 std::cin >> pitchT;  
 #ifdef PitchThreshold  
  #undef PitchThreshold  
  #define PitchThreshold pitchT   
  #endif  

我的问题是:
以这种方式使用 #undef 是否可以确保在所有源文件中重新定义宏,或者它对于编写上述代码行的函数来说是本地的吗? #undef 和 #define 运算符的范围是什么?
我可以做什么(除了更改文件中 #define-d 本身的宏之外)来确保在所有源文件中更改宏定义?
谢谢,
斯里拉姆

I have a question about using #undef to redefine macros.
I have a file global.h which contains a number of #define-d macros. In the code that uses these macros, I find that the values that the macros hold are not generic enough. I want to redefine the macros to make them more generic. I wrote the following code snippet to do just that:

 std::cout << endl << "Enter pitch threshold:" << endl;  
 std::cin >> pitchT;  
 #ifdef PitchThreshold  
  #undef PitchThreshold  
  #define PitchThreshold pitchT   
  #endif  

My questions are:
Does using a #undef in this manner ensure redefinition of the macro across all source files, or is it local to the function where the above lines of code are written? What is the scope of the #undef and #define operators?
What can I do (apart from changing the macros in the file where they are #define-d itself) to ensure that the macro definitions are changed across all source files?
Thanks,
Sriram

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘虞初梦 2024-10-07 05:57:45

#ifdef 是一个预处理器指令,这意味着它将在编译源代码之前应用。这意味着只有“下面”的源代码会受到影响。如果您通过预处理器运行源代码,您将能够看到结果。这将使您更深入地了解预处理器的工作原理。

#ifdef is a preprocessor directive, this means that it will be applied before your source code is compiled. It means that only the source code 'below' will be affected. If you run your source code through the preprocessor you'll be able to see the result. That will give you more insight in the workings of the preprocessor.

去了角落 2024-10-07 05:57:45

#undef 运算符的作用范围是调用后的整个文件。这包括包含它的所有文件(因为预处理器只是将文件链接在一起。)因为它是预处理器的一部分,所以它没有范围之类的奇怪的东西。

The scope of the #undef operator is the whole file after it's called. This includes all files that include it (because the preprocessor just chains the files together.) Because it's part of the preprocessor it doesn't have weird things like scope.

暗藏城府 2024-10-07 05:57:45

任何#define宏都不是本地变量,也不是全局变量,而是一个“类似常量”的表达式,因此,即使某些DEFINED_VALUE将在之后的任何地方#undefined - 这取消定义的范围将从其位置到该源文件的末尾

它具有非常简单的逻辑,但由于任何函数内部和外部都允许定义,因此可能会造成混淆。

回答最后一个问题:

我们无法真正轻松地重新定义宏——在全局范围内。不舒服的方法是将所有代码放在一个文件中。

在实践中可以做的是一种默认的事情——新的变量声明(在 main() 函数的顶部)具有类似的命名,以便与定义的值并行使用。

any #define macros is not local, nor global variable, but a "constant-like" expression, so, even if some DEFINED_VALUE will be #undefined anywhere after - this un-definition will be scoped from its place to the end of this source-file.

it has pretty straight-forward logic, but may confuse due to definitions being allowed both inside and outside of any functions.

Answering last question:

We can't really comfortably redefine a macros — in mean of global scope. Uncomfortable way is to place all the code in a single file.

What can be done in practice is kinda default thing — new variable declaration (at the top of the main() function) with similar naming to use it in parallel with defined value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文