本文介绍了解析void()和int()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

在阅读了最令人头疼的语法分析之后,我做了一些实验,找到了这个程序.有两条非常相似的线.其中之一在g ++ 7和clang ++-3.9中都发出警告,而另一种则没有.

Having read about the most vexing parse, I experimented a bit and found this program. There are two very similar lines. One of them yields warnings in both g++7 and clang++-3.9, another does not.

int main() {
  void(); // no warning
  int(); // warning: statement has no effect
}

在第二行中,创建了类型为int的默认构造的对象,并立即将其销毁,因此未使用.但是第一行会发生什么?如果以相同的方式进行解析,则应该是错误的,因为创建类型为void的对象是非法的.另一方面,它看起来也不像是函数声明.

In the second line a default-constructed object of type int is created and immediately destroyed, thus unused. But what happens in the first line? If it was parsed the same way, it should be an error because it is illegal to create an object of type void. On the other hand, it does not look like a function declaration as well.

推荐答案

解析没有区别.两种情况都由简单类型说明符覆盖,后跟可选的带括号的 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:

这特别说明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.

这篇关于解析void()和int()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 09:57