【发布时间】:2017-03-29 14:33:52
【问题描述】:
我有一小段代码,其中有一个声明 void();
int main()
{
void( ); // 1: parses fine in GCC 5.4.0 -Wpedantic
// void; // 2: error declaration does not declare anything
}
1 void() 到底是什么?
- 匿名函数声明?
- 类型声明?
- 空表达式?
是什么让 1 void() 与 2 void; 不同?
我已经读过了:
- Is sizeof(void()) a legal expression? 但 void() 被认为是 sizeof 中的一种类型
- What does the void() in decltype(void()) mean exactly? 在 declspec 中考虑。
- 我读到了Is void{} legal or not?
但我很好奇松散的声明 void();不同于其中之一(当然是为什么)
【问题讨论】:
-
我怀疑 void() 类似于例如int pi = int(3.14); ...除了将 3.14 转换为 int 之外,您将空表达式转换为 void 类型(然后忽略结果,因为它是 void 类型,所以无论如何您都必须这样做)。
-
相关:stackoverflow.com/questions/39279074/… 我正在努力寻找更好的骗子
-
可能在模板中很有用,这样您就不必专门研究 void?
-
对于那些将我指向可能的骗子的人:大多数相关问题都是由用户 skypjack 提出的,类似于我在问题中提出的问题。但是我还没有看到“松散声明” void(); 的情况。被考虑,但仅在 decltype、sizeof 等的上下文中。
标签: c++ language-lawyer