简体   繁体   English

为什么在C ++中使用(void)1作为无操作?

[英]Why use (void)1 as a no-op in C++?

I'm reviewing a third party codebase and see this definition of an assert macro: 我正在审查第三方代码库并看到assert宏的这个定义:

#define assert( x ) \
     if( !( x ) ) { \
        ThrowException( __FILE__, __LINE__ ); \
     } else \
        ((void)1)

What's the point in (void)1 ? (void)1什么意义? How is it better than idiomatic (void)0 ? 它比idiomatic (void)0更好吗?

(void)1(void)0之间没有区别。

I think it does not matter that much (and will be optimized away by the compiler). 我认为这并不重要(并且将由编译器优化)。 And <cassert> is a standard C++ header (using the standard <assert.h> C header) which defines a standard assert macro, so an application should not re-define it. <cassert>是一个标准的C ++头文件(使用标准的<assert.h> C头文件),它定义了一个标准的assert宏,因此应用程序不应该重新定义它。

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

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