简体   繁体   English

如何在C ++中实现no-op宏(或模板)?

[英]How do I implement no-op macro (or template) in C++?

How do I implement no-op macro in C++? 如何在C ++中实现no-op宏?

#include <iostream>   

#ifdef NOOP       
    #define conditional_noop(x) what goes here?   
#else       
    #define conditional_noop(x) std::cout << (x)   
#endif   
int main() {       
    conditional_noop(123);   
}

I want this to do nothing when NOOP is defined and print "123", when NOOP is not defined. 我希望在定义NOOP时不执行任何操作,并且在未定义NOOP时打印“123”。

While leaving it blank is the obvious option, I'd go with 虽然留空是显而易见的选择,但我会选择

#define conditional_noop(x) do {} while(0)

This trick is obviously no-op, but forces you to write a semicolon after conditional_noop(123) . 这个技巧显然是无操作,但强迫你在conditional_noop(123)之后写一个分号。

As mentioned before - nothing. 如前所述 - 没有。
Also, there is a misprint in your code. 此外,您的代码中存在错误打印。
it should be #else not #elif . 它应该是#else而不是#elif if it is #elif it is to be followed by the new condition 如果它是#elif ,则后面跟着新条件

#include <iostream>   

#ifdef NOOP       
    #define conditional_noop(x) do {} while(0)
#else       
    #define conditional_noop(x) std::cout << (x)   
#endif  

Have fun coding! 玩得开心! EDIT: added the [do] construct for robustness as suggested in another answer. 编辑:添加[do]构造的稳健性,如另一个答案所示。

Defining the macro to be void conveys your intent well. 将宏定义为void可以很好地传达您的意图。

#ifdef NOOP
    #define conditional_noop(x) (void)0
#else
 #ifdef NOOP       
     #define conditional_noop(x)   
 #elif  

nothing! 没有!

#ifdef NOOP
    static inline void conditional_noop(int x) { }
#else 
    static inline void conditional_noop(int x) { std::cout << x; }
#endif

Using inline function void enables type checking, even when NOOP isn't defined. 即使未定义NOOP ,使用内联函数void也可以进行类型检查。 So when NOOP isn't defined, you still won't be able to pass a struct to that function, or an undefined variable. 因此,当未定义NOOP时,您仍然无法将结构传递给该函数或未定义的变量。 This will eventually prevent you from getting compiler errors when you turn the NOOP flag on. 这将最终阻止您在打开NOOP标志时收到编译器错误。

You can just leave it blank. 你可以把它留空。 You don't need to follow the #define with anything. 您无需关注#define

Like others have said, leave it blank. 像其他人说的那样,留空。

A trick you should use is to add (void)0 to the macro, forcing users to add a semicolon after it: 你应该使用的一个技巧是向宏添加(void)0 ,强制用户在它之后添加一个分号:

#ifdef NOOP       
    #define conditional_noop(x) (void)0
#else       
    #define conditional_noop(x) std::cout << (x); (void)0
#endif  

In C++, (void)0 does nothing. 在C ++中, (void)0什么都不做。 This article explains other not-as-good options, as well as the rationale behind them. 本文将介绍其他不太好的选项,以及它们背后的基本原理。

As this is a macro, you should also consider a case like 由于这是一个宏,你还应该考虑一个例子

if (other_cond)
    conditional_noop(123);

to be on the safe side, you can give an empty statement like 为了安全起见,你可以给出一个空洞的陈述

#define conditional_noop(X) {}

for older C sometimes you need to define the empty statment this way (should also get optimized away): 对于较旧的C,有时您需要以这种方式定义空状态(也应该进行优化):

#define conditional_noop(X) do {} while(0)

I think that a combination of the previous variants is a good solution: 我认为以前的变种组合是一个很好的解决方案:

#ifdef NOOP
    static inline void conditional_noop(int x) do {} while(0)
#else 
    static inline void conditional_noop(int x) do { std::cout << x; } while(0)
#endif

The good thing is that these two codes differ only inside a block , which means that their behaviour for the outside is completely identical for the parser. 好处是这两个代码只在一个块内有所不同,这意味着它们对于外部的行为对于解析器来说是完全相同的。

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

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