https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110197
Bug ID: 110197
Summary: Empty constexpr object constructor erronously claims
out of range access
Product: gcc
Version: 13.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: matt at godbolt dot org
Target Milestone: ---
The following code, compiled on GCC 13.1 complains of a non-constexpr safe
array access:
```
#include <array>
#include <string_view>
struct Currency {
// commenting-in the `i` below fixes, as does passing a `const char *` to
the ctor
// int i{};
constexpr explicit Currency(std::string_view name) {}
// constexpr explicit Currency(const char *name) {}
};
static Currency get_c() {
static constexpr std::array c{Currency{"BWP"}};
return c[0];
}
```
The error is:
```
<source>: In function 'Currency get_c()':
<source>:12:50: in 'constexpr' expansion of
'Currency(std::basic_string_view<char>(((const char*)"BWP")))'
<source>:12:50: error: accessing uninitialized array element
12 | static constexpr std::array c{Currency{"BWP"}};
|
```
The same code compiles without issue on GCC 12.1, and on clang. Passing either
a const char * to the constructor, or adding a single field to the class makes
it compile.
https://godbolt.org/z/G355frEM4
Was also reproducible on trunk as of 14da764809d3bffe9336e72999312ba3fb019dc6