Can you initialize a "cv char[]" with a string literal?
Asked Answered
K

0

8

The current wording in [dcl.init.string] p1 states:

An array of ordinary character type, [...] may be initialized by an ordinary string literal, [...], or by an appropriately-typed string-literal enclosed in braces.

Ordinary character type includes char, signed char, and unsigned char (see [basic.fundamental]). It does not include the cv-qualified versions of those types.

From this, it isn't clear whether an array of const char or volatile char may also be initialized using a string literal, not just an array of char. The wording is unclear because it doesn't say:

An array of cv ordinary character type, [...]

This matters in the following situation:

const char str[] = "test"; // is this valid?

I'm 99% sure that the intention is that you should be able to do it. However, is there a paragraph that definitively allows you to, or is this a wording defect?

Kubetz answered 21/8, 2023 at 10:59 Comment(5)
That's an array of ordinary character type, char const[]. What am I missing?Folkmoot
@Folkmoot I guess the question asks if cv-qualified ordinary character type is still ordinary character type. I'd expect wording like "An array of (possibly cv-qualified) ordinary character type [...]"Dropper
Addendum: a string literal is already const-qualified. Does not say anything about volatile-qualification oc. See eel.is/c++draft/lex.string#1Ovoviviparous
@Folkmoot What am I missing? The definition of ordinary character typeKorrie
Related to CWG251, CWG2185, CWG2448Korrie

© 2022 - 2024 — McMap. All rights reserved.