https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77841
Bug ID: 77841
Summary: a new expression of a char array cannot be initialized
by a string literal
Product: gcc
Version: 7.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: kariya_mitsuru at hotmail dot com
Target Milestone: ---
Please see the sample code below.
============== sample code ==============
int main()
{
char* s = new char[4]{"abc"};
}
============== sample code ==============
===== compiler output (GCC HEAD 7.0.0 20161003) =====
prog.cc: In function 'int main()':
prog.cc:3:32: error: invalid conversion from 'const char*' to 'char'
[-fpermissive]
char* s = new char[4]{"abc"};
^
===== compiler output (GCC HEAD 7.0.0 20161003) =====
cf. http://melpon.org/wandbox/permlink/utGUZYo8NKRqGDM5
The latest draft standard says,
5.3.4 New [expr.new] p.17.2
Otherwise, the new-initializer is interpreted according to the initialization
rules of 8.5 for direct-initialization.
8.5 Initializers [dcl.init] p.17.1
If the initializer is a (non-parenthesized) braced-init-list, the object or
reference is list-initialized (8.5.4).
8.5.4 List-initialization [dcl.init.list] p.3.2
Otherwise, if T is a character array and the initializer list has a single
element that is an appropriately-typed string literal (8.5.2), initialization
is performed as described in that section.
So, I think that the sample code above should be compiled successfully.
Note that 8.5.4 p.3.2 was added for resolution of
DR1490.(http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1490) and
DR1467.(http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1467).
Note also that GCC (at least 4.4.7 or later) accepts a string-literal within a
brace-init-list for (usual) direct-initialization.
============== sample code ==============
int main()
{
char s[4]{"abc"};
}
============== sample code ==============
cf. http://melpon.org/wandbox/permlink/fZkVTLQXBxatUjBX