Skip to content

CWG2977 [dcl.init.string] Clarify syntactic forms of initializers that are considered as string literals #661

Description

@frederick-vs-ja

Full name of submitter (unless configured in github; will be published with the issue): Jiang An

Reference (section label): [dcl.init.general], [dcl.init.string]

[dcl.init.general] p16.3:

If the destination type is an array of characters, an array of char8_t, an array of char16_t, an array of char32_t, or an array of wchar_t, and the initializer is a string-literal, see [dcl.init.string].

[dcl.init.string] p1

An array of ordinary character type ([basic.fundamental]), char8_t array, char16_t array, char32_t array, or wchar_t array may be initialized by an ordinary string literal, UTF-8 string literal, UTF-16 string literal, UTF-32 string literal, or wide string literal, respectively, or by an appropriately-typed string-literal enclosed in braces ([lex.string]). [...]

The syntactic forms of such initializers are not very clear and there's implementation divergence.

"Good" forms:

  • char a[] = "foo"; - accepted by all known implementations, and clarified to be valid in notes;
  • char a[]("foo"); - accepted by all known implementations;
  • char a[]{"foo"}; - accepted by all known (C++11 and later) implementations;
  • char a[] = {"foo"}; - accepted by all known (C++11 and later) implementations.

Controversial forms with additonal parentheses:

Weird forms with additional braces:

  • char arr[]{{"foo"}}; - accepted by MSVC and rejected by others;
  • char arr[] = {{"foo"}}; - same as above.

It seems that we should clarify the permitted syntactic forms, which should include "good" and controversial forms and exclude weird forms.

Link to reflector thread (if any):

Issue description:

Suggested resolution:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions