Date: Tue, 6 May 2025 17:10:15 +0200
On 06/05/2025 15.39, Vincent Mailhol wrote:
> Hi Jens,
>
> On Mar. 6 May 2025 at 02:42, Jens Maurer <jens.maurer_at_[hidden]> wrote:
>> On 05/05/2025 18.24, Jāāā Gustedt via Liaison wrote:
>
> (...)
>
>> Indeed, but
>>
>> ( (void)1, 1 )
>>
>> does not cause problems with the syntax for conditional-expression.
>>
>> But there is an extra rule in C23 6.6 paragraph 3:
>>
>> "Constant expressions shall not contain assignment, increment, decrement, function-call, or comma
>> operators, except when they are contained within a subexpression that is not evaluated."
>>
>>> What the historical reason
>>> for this would be, I don't know, and if would be easy to change I
>>> don't know either.
>>
>> Having static_assert(x) be an expression of void type and then just writing
>>
>> ( static_assert(whatever), x )
>>
>> seems like the most user-friendly syntax.
>> (If you're in a macro, you likely want some outer parentheses anyway.)
>
> I do not think that this works in C23. As far as I can see, when writing:
>
> ;
>
> the expression is not an integer constant expression, even with the
> extra parentheses. At least, both GCC and clang complain about it.
> Here are a few extra examples:
>
> https://godbolt.org/z/YqM45MjPz
Yes, those are the current C rules. I suggest that WG14 relax the rules
in 6.6 p3 about which operators may appear inside a constant-expression,
permitting ((void)1, 1) to be an integer constant expression.
> But at the end, my point it that if static_assert() yields an integer,
> it can be used with both the comma operator and with other operator
> (such as the +, c.f. prior works). So, yielding an integer does not
> close any doors.
I think having to write something like
( static_assert(x > 0) + static_assert(x < 99) + x + 3 )
is visually inferior to writing
( static_assert(x > 0), static_assert(x < 99), x + 3 )
In the former case, there is no actual sum of four numbers being
computed; there are two static assertions plus a sum of two numbers.
Jens
> Hi Jens,
>
> On Mar. 6 May 2025 at 02:42, Jens Maurer <jens.maurer_at_[hidden]> wrote:
>> On 05/05/2025 18.24, Jāāā Gustedt via Liaison wrote:
>
> (...)
>
>> Indeed, but
>>
>> ( (void)1, 1 )
>>
>> does not cause problems with the syntax for conditional-expression.
>>
>> But there is an extra rule in C23 6.6 paragraph 3:
>>
>> "Constant expressions shall not contain assignment, increment, decrement, function-call, or comma
>> operators, except when they are contained within a subexpression that is not evaluated."
>>
>>> What the historical reason
>>> for this would be, I don't know, and if would be easy to change I
>>> don't know either.
>>
>> Having static_assert(x) be an expression of void type and then just writing
>>
>> ( static_assert(whatever), x )
>>
>> seems like the most user-friendly syntax.
>> (If you're in a macro, you likely want some outer parentheses anyway.)
>
> I do not think that this works in C23. As far as I can see, when writing:
>
> ;
>
> the expression is not an integer constant expression, even with the
> extra parentheses. At least, both GCC and clang complain about it.
> Here are a few extra examples:
>
> https://godbolt.org/z/YqM45MjPz
Yes, those are the current C rules. I suggest that WG14 relax the rules
in 6.6 p3 about which operators may appear inside a constant-expression,
permitting ((void)1, 1) to be an integer constant expression.
> But at the end, my point it that if static_assert() yields an integer,
> it can be used with both the comma operator and with other operator
> (such as the +, c.f. prior works). So, yielding an integer does not
> close any doors.
I think having to write something like
( static_assert(x > 0) + static_assert(x < 99) + x + 3 )
is visually inferior to writing
( static_assert(x > 0), static_assert(x < 99), x + 3 )
In the former case, there is no actual sum of four numbers being
computed; there are two static assertions plus a sum of two numbers.
Jens
Received on 2025-05-06 15:10:23