C++ 空函数宏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9187628/
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
Empty function macros
提问by Qix - MONICA WAS MISTREATED
If I define a function macro with no actual body, is it like an empty string with the compiler (i.e. It doesn't generate any extra instructions at compile time)?
如果我定义了一个没有实际主体的函数宏,它是否像编译器中的空字符串(即它在编译时不生成任何额外指令)?
Example:
例子:
#define SomeMacro(a)
SomeMacro("hello"); // This line doesn't add any instructions, does it?
回答by Mark Ransom
You're absolutely correct, the empty macro doesn't generate any code.
你是绝对正确的,空宏不会生成任何代码。
I've seen two places where this is useful. The first is to eliminate warnings when a function parameter isn't used:
我已经看到两个地方这很有用。第一个是在不使用函数参数时消除警告:
#define UNUSED(x)
int foo(int UNUSED(value))
{
return 42;
}
The second is when you use conditionals to determine if there should be code or not.
第二个是当您使用条件来确定是否应该有代码时。
#ifdef LOGGING_ENABLED
#define LOG(x) log_message(x)
#else
#define LOG(x)
#endif
回答by CoffeDeveloper
Your code is not totally correct, I suggest you to put empty braces in your macro
您的代码不完全正确,我建议您在宏中放置空大括号
#define somemacro(a) {}
the reason is simple, you code will be much more safe!
原因很简单,你的代码会安全很多!
take this example:
拿这个例子:
if(Value)
somemacro(a)
else
somemacro(b)
If the macro is empty, your code will not compile! (Expected primary-expressione before "else"). Anyway certain style rules force you to write
如果宏为空,您的代码将无法编译!(“else”之前的预期主要表达式)。无论如何,某些样式规则迫使您编写
if(Value)
{
somemacro(a)
}
else
{
somemacro(a)
}
so that will not be a problem.
所以这不会有问题。
Another option is to use ";" instead of "{}" but that option in the same case will give you compile time warnings, while empty braces will not give warnings nor errors! (semicolon is still better even if give warnings) ;)
另一种选择是使用“;” 而不是“{}”,但同一情况下的该选项会给你编译时警告,而空大括号不会给出警告或错误!(即使给出警告,分号仍然更好);)
take following case
采取以下案例
if(value)
somemacro(a);
else
somemacro(b);
will expand to
将扩展到
if(value)
{};
else
{};
that can't compile!
不能编译!
That's why macros are evil
这就是为什么宏是邪恶的
(since macros are simple text-replacement, the rule of dumbs should be to always try to manually replace the code and see what happens, there are also tools that will replace macros for you showing the expanded code.)
(由于宏是简单的文本替换,笨蛋的规则应该是始终尝试手动替换代码并查看会发生什么,还有一些工具可以为您显示扩展代码来替换宏。)
Still guessin if there is a macro that is totally safe? Yes it is called "NOP"
还在猜测是否有一个完全安全的宏?是的,它被称为“NOP”
#define somemacro(a) ((void)0)
that will work in any case (even source files of compilers use that as NOP, for example just look at "assert.h"
这在任何情况下都可以工作(甚至编译器的源文件也将其用作 NOP,例如只需查看“assert.h”
回答by Cody Gray
The preprocessor performs literal substitution with all macros.
预处理器使用所有宏执行文字替换。
Therefore, if you define an "empty" macro, then each place that identifier appears in your code will be replaced with an empty statement by the preprocessor before the compiler ever runs.
因此,如果您定义了一个“空”宏,那么在编译器运行之前,该标识符出现在您代码中的每个位置都将被预处理器替换为一个空语句。
So yes. No code will be generated for the example given in your question.
所以是的。不会为您的问题中给出的示例生成任何代码。
回答by Greg Hewgill
That's correct. Your code expands to
没错。您的代码扩展为
;
after preprocessing.
预处理后。
Note that you can ask your compiler to show you the code afterpreprocessing (in gcc, this is the -E
option; your compiler may vary).
请注意,您可以要求编译器在预处理后向您显示代码(在 gcc 中,这是-E
选项;您的编译器可能会有所不同)。