Skip to content

Constant evaluation rejects "new wchar_t[n]{L\"...\"}" although n is large enough #501

Closed

Description

@daveedvdv-nvidia

During constant evaluation, the check that the initializer of an array
new-expression fits in the allocated array uses the size in bytes of a string
literal initializer instead of its number of characters. So when the elements
are wider than one byte, an array that is large enough is reported as too
small:

constexpr int f(int n) {
  auto p = new wchar_t[n]{L"ab"};
  int r = p[1];
  delete[] p;
  return r;
}
static_assert(f(3) == L'b');
error: expression must have a constant value
note: number of dynamically-allocated elements (3) too small for initializer

The same happens with char16_t and char32_t arrays; char and char8_t
arrays are not affected. (Found by CodeRabbit while reviewing
#495.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions