Difference between parsing of void() and int()

旧城冷巷雨未停 提交于 2019-11-30 17:48:26

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

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

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.

This specifically says that void() is a prvalue of type 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!

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.


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.

It is parsed the same way.

Warnings don't come from the parser. They arise during semantic analysis. The SA noticed that a value was created an destroyed by int(); without being read or written.

In the void case, there is no value, so no warning.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!