这是 this 的后续行动问题。
在评论和答案中,不止一次提到 void{}
既不是有效的类型 ID,也不是有效的表达式。
这很好,很有意义,仅此而已。
然后我通过 [7.1.7.4.1/2] (占位符类型扣除)工作草案。
据说:
[...]
- for a non-discardedreturn
statement that occurs in a function declared with a return type that contains a placeholder type,T
is the declared return type ande
is the operand of thereturn
statement. If thereturn
statement has no operand, thene
isvoid{}
;
[...]
那么,void{}
(在概念上)是否合法?
如果工作草案中提到它是可以接受的(即使只是作为一个 - 就好像它是一个 - 声明),它必须确实是合法的。这意味着 decltype(void{})
也应该是有效的,例如。
否则,工作草案是否应该使用 void()
而不是 void{}
?
好吧,老实说,我很确定我不够熟练,无法指出工作草案中的错误,所以真正的问题是:我的推理出了什么问题?
上面项目符号中提到的 void{}
究竟是什么?为什么在这种情况下它是合法的表达方式?
最佳答案
对我来说,这听起来像是有人将旧标准与新标准合并在一起时搞砸了。
以前的标准是这样说的:(C++14 N4140, 7.1.6.4.7 [dcl.spec.auto]):
When a [...]
return
statement occurs in a function declared with a return type that contains a placeholder type, the deduced return type or variable type is determined from the type of its initializer. In the case of areturn
with no operand, the initializer is considered to bevoid()
.
较新的标准允许 if constexpr
语句,因此需要更改语言以反射(reflect)这一点。 if constexpr
导致了一个潜在的discarded return
语句的概念(如果 return
在 not-taken constexpr if 的分支,则将其丢弃,并从其他返回语句推断返回类型(如果有)。
可能新的措辞应该是这样的:
for a non-discarded return statement that occurs in a function declared with a return type that contains a placeholder type,
T
is the declared return type ande
is the operand of thereturn
statement. If the return statement has no operand, thenT
isauto
and the deduced return type isvoid
关于c++ - void{} 是否合法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39354065/