简体   繁体   English

具有void(0)的C ++指令

[英]C++ Directive With void(0)

What is the purpose of having a directive like below? 拥有如下所示的指令的目的是什么?

#define TEST_CONDITION(con) !(con) ? (void)0:

In particular, I see this called at the start of other directives. 特别是,我看到这在其他指令的开头被调用。

For example, 例如,

#define OTHER_CONDITION(..)
  TEST_CONDITION(someFunction)
  ANOTHER_DIRECTIVE(...)

Doesn't TEST_CONDITION just no-op or a boolean is returned that isn't used in these cases? TEST_CONDITION是否只返回no-op或在这些情况下不使用的布尔值?

Expand the macro, and it becomes clearer. 展开宏,它将变得更加清晰。 I'll also use some formatting to keep the code readable, and I assume that the lack of some essential escape characters is not meant to be part of the example. 我还将使用某种格式来保持代码的可读性,并且我认为缺少一些必要的转义符并不意味着要成为示例的一部分。 OTHER_CONDITION becomes: OTHER_CONDITION变为:

!(someFunction)
    ? (void)0
    : ANOTHER_DIRECTIVE(...)

So, the expression someFunction is executed, and if it is true , then ANOTHER_DIRECTIVE(...) (or whatever it expands to) is executed. 因此,执行了someFunction表达式,如果该表达式为true ,则执行ANOTHER_DIRECTIVE(...) (或扩展为它的任何值)。 Otherwise nothing is executed. 否则不执行任何操作。


Simpler way to write OTHER_CONDITION could be: 编写OTHER_CONDITION更简单方法是:

#define OTHER_CONDITION(..) if(someFunction) ANOTHER_DIRECTIVE(...)

This simplification lacks some restrictions that TEST_CONDITION provides: 这种简化没有TEST_CONDITION提供的一些限制:

  • TEST_CONDITION makes it impossible to append an else branch. TEST_CONDITION使得无法追加else分支。
  • TEST_CONDITION makes it ill-formed to use a non-void ANOTHER_DIRECTIVE(...) . TEST_CONDITION使得使用非无效的ANOTHER_DIRECTIVE(...)ANOTHER_DIRECTIVE(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM