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: Sat, 28 Dec 2024 00:51:48 +0100
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:

  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.

> Is that just a widespread bug?)

That would be an extension, not a bug, I think.

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-27 23:51:52