且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

解析void()和int()之间的区别

更新时间:2023-02-19 22:01:58

解析没有区别.两种情况都由简单类型说明符覆盖,后跟可选的带括号的 expression-list .

There is no difference in parsing. Both cases are covered by simple-type-specifier followed by optional parenthesized expression-list.

语义含义在C ++ 17(N4659)[expr.type.conv]/2中指定:

The semantic meaning is specified in C++17 (N4659) [expr.type.conv]/2:

如果类型为 cv void且初始值设定项为(),则表达式为指定类型的prvalue,不执行初始化.否则,表达式是指定类型的prvalue,其结果对象将使用初始化程序直接初始化.

If the type is cv void and the initializer is () , the expression is a prvalue of the specified type that performs no initialization. Otherwise, the expression is a prvalue of the specified type whose result object is direct-initialized with the initializer.

这特别说明void()是类型void的prvalue.

This specifically says that void() is a prvalue of type void.

现在,我敢肯定,void类型的prvalue不是非法的,因为这是常见的情况,例如(void)x;或调用void函数!

Now, I'm sure that it is not intended that a prvalue of type void be illegal, as it is a common occurrence, e.g. (void)x; or calling a void function!

但是我找不到标准中的哪个地方说应该抑制void prvalue的临时实现. [class.temporary]/2似乎说一个废弃值表达式总是实现一个临时值.而实现不完整类型的prvalue是错误的.也许这是标准中的缺陷.

But I can't find where in the Standard it says that temporary materialization should be suppressed for void prvalues. The [class.temporary]/2 seems to say that a discarded-value expression always materializes a temporary; and it is an error to materialize a prvalue of incomplete type. Maybe it is a defect in the standard.

关于未使用的值"的警告的不同之处可能是因为void类型的未使用的值是常见的情况,因此不会发出警告.

The difference in warning about "unused value" is probably because an unused value of type void is a common occurrence and it would not be helpful to warn about.