https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122978
Bug ID: 122978
Summary: cannot create constexpr std::string variable with
short string in local scope
Product: gcc
Version: 15.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: fchelnokov at gmail dot com
Target Milestone: ---
This program
```
#include <string>
int main() {
constexpr std::string s = "a";
}
```
is accepted by Clang with libc++ and by MSVC. But with libstdc++ the
compilation fails:
<source>:4:31: error:
'std::string{std::__cxx11::basic_string<char>::_Alloc_hider{((char*)(&
s.std::__cxx11::basic_string<char>::<anonymous>.std::__cxx11::basic_string<char>::<unnamed
union>::_M_local_buf))}, 1, std::__cxx11::basic_string<char>::<unnamed
union>{char [16]{'a', 0, '\000', '\000', '\000', '\000', '\000', '\000',
'\000', '\000', '\000', '\000', '\000', '\000', '\000', '\000'}}}' is not a
constant expression
4 | constexpr std::string s = "a";
| ^~~
<source>:4:31: error: 'std::__cxx11::basic_string<char>(((const char*)"a"),
std::allocator<char>())' is not a constant expression because it refers to an
incompletely initialized variable
Online demo: https://gcc.godbolt.org/z/Wv9PvEnjM
Related discussion: https://stackoverflow.com/q/79835432/7325599