https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120551
Bug ID: 120551
Summary: Dynamic char array allocation with string literal in
brace initialization fails to use the initialization
if size provided is not constexpr
Product: gcc
Version: 12.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: kirshamir at gmail dot com
Target Milestone: ---
Dynamic char array allocation with string literal in brace initialization fails
to use the initialization if size provided is not constexpr.
Since gcc 12.1 this code is allowed:
constexpr size_t size = 2;
auto test1 = new char[size]{"a"};
As it should be allowed based on
https://timsong-cpp.github.io/cppwp/expr.new#8.4
(The size is valid, thus should be ok).
However, if size is initialized without constexpr, test1 is not initialized
correctly (doesn't copy "a" into the array):
size_t size = 2;
auto test1 = new char[size]{"a"};
Code:
gcc 12.1
https://compiler-explorer.com/z/fsv9MxvoT
gcc 15.1
https://compiler-explorer.com/z/x9x789sx1
trunk:
https://compiler-explorer.com/z/q7fe5avn6
Source:
https://stackoverflow.com/questions/79653388/issue-with-aggregate-initialization-with-the-new-operator