Is void{} legal or not?

前端 未结 2 1031
醉酒成梦
醉酒成梦 2020-12-30 21:23

This is a follow-up up of this question.
In the comments and in the answer it is said more than once that void{} is neither a valid type-id nor a valid

2条回答
  •  -上瘾入骨i
    2020-12-30 21:50

    To me it sounds like someone messed up merging the previous standard with the new one.

    Previously the standard said this: (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 a return with no operand, the initializer is considered to be void().

    The newer standard allows for if constexpr statements, so the language needed to change to reflect that. if constexpr leads to the concept of a potentially discarded return statement (if the return is in the not-taken branch of a constexpr if, then it's discarded and the return type is inferred from other return statements, if any).

    Probably the new wording should be something like:

    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 and e is the operand of the return statement. If the return statement has no operand, then T is auto and the deduced return type is void

提交回复
热议问题