UB22⚓︎
Author: Yikai Cui.
Definition⚓︎
An attempt is made to use the value of a void expression, or an implicit or explicit conversion (except to void) is applied to a void expression (6.3.2.2).
尝试使用一个void表达式的值,或者对void表达式的直接或隐含的向非void之外的类型转换。
Description⚓︎
The (nonexistent) value of a void expression (an expression that has type void) shall not be used in any way, and implicit or explicit conversions (except to void) shall not be applied to such an expression. If an expression of any other type is evaluated as a void expression, its value or designator is discarded. (A void expression is evaluated for its side effects.) (from ISO/IEC 9899:2023(E) 6.3.2.2, 1)
void表达式(类型为void的表达式)的值(并不存在)不应该以任何形式被使用;并且除非转换到void,不应该对此表达式做现式或隐式的类型转换。如果一个具有其他类型的表达式被作为void表达式求值,它的值会被舍去。
Note⚓︎
Nearly all (Standard conforming) implementations consider using void expression and void expression conversions as error, and will raise error during compilation.
Advice⚓︎
Mainstream IDEs provide static code analysis tools that can discover invalid void usage and void conversions. If IDE warns, double check to confirm.