C++ Logo

liaison

Advanced search

Re: [isocpp-wg14/wg21-liaison] Array initialization with parentheses-enclosing string literals

From: Jₑₙₛ Gustedt <jens.gustedt_at_[hidden]>
Date: Sun, 29 Dec 2024 09:33:21 +0100
Jens,

on Sun, 29 Dec 2024 00:43:09 +0100 you (Jens Maurer
<jens.maurer_at_[hidden]>) wrote:

> On 28/12/2024 00.51, Jₑₙₛ Gustedt wrote:
> > Jens,
> >
> > on Sat, 28 Dec 2024 00:26:41 +0100 you (Jens Maurer via Liaison
> > <liaison_at_[hidden]>) wrote:
> >
> >> const char s1[] = "abc"; // #1
> >> const char s2[] = ("abc"); // #2
> >>
> >> #1 is the canonical way to initialize a character array with
> >> a string literal, and is supported by both C and C++.
> >>
> >> Recently, a question came up whether #2 is valid, too.
> >> C++ uses grammar non-terminals in this area of the specification,
> >> and it seems pretty clear that parenthesized string literals don't
> >> satisfy the rules, and thus #2 is ill-formed.
> >>
> >> C23 superficially has the same rules as C++ in this area,
> >> but I'd appreciate some confirmation.
> >
> > The corresponding phrase only refers to string literals themselves
> > (the syntax element) and not expressions, as would be a string
> > literal enclosed in parenthesis, 6.7.11 p7, first sentence:
>
> Ok, so we both have the same understanding of the C23 rules.
>
> > The initializer for an array shall be either a string literal,
> > optionally enclosed in braces, or a brace-enclosed list of
> > initializers for the elements.
> >
> >> (Quite a few implementations appear to accept #2.
> >
> > do they ? I never would have suspected this to work.
>
> Yes, unanimously so (as far as C++-related implementations
> are concerned):
>
> https://godbolt.org/z/EPTrn1PdG

It seems that it only appeared recently in clang (18 or so), which I
find surprising. Perhaps people refer to

   A parenthesized expression is a primary expression. Its type, value,
   and semantics are identical to those of the unparenthesized
   expression.

But this widening says nothing about syntax so it does not apply, here.

Thanks
Jₑₙₛ

-- 
:: ICube :::::::::::::::::::::::::::::: deputy director ::
:: Université de Strasbourg :::::::::::::::::::::: ICPS ::
:: INRIA antenne de Strasbourg :::::::::::::::::: Camus ::
:: INRIA PIQ program Strasbourg :::::::::: piq.inria.fr ::
:: :::::::::::::::::::::::::::::::::::: ☎ +33 368854536 ::
:: https://icube-icps.unistra.fr/index.php/Jens_Gustedt ::

Received on 2024-12-29 08:33:26