这是像 learncpp.com 声称的 GCC 错误吗?

Is this a GCC bug like learncpp.com claims?

C++学习网站(https://www.learncpp.com/cpp-tutorial/6-9a-dynamically-allocating-arrays/)声称GCC在涉及new运算符时存在C风格字符串初始化相关的bug:

As of the time of writing, the GCC still has a bug where initializing a dynamically allocated array of chars using a C-style string literal causes a compiler error:

char *array = new char[14] { "Hello, world!" }; // doesn't work in GCC, though it should

我正在使用 GCC 7.4.0 版。

如果我尝试编译这段摘录,我会得到:

error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive]
     char *array = new char[14] { "Hello, world!" };

如果我尝试编译这样的代码:

char *array = new char[14] {'H', 'e', 'l', 'l', 'o', 0}

然后它按预期工作。

我假设这被认为是一个错误,因为:

char array[14]{ "Hello, world!" };

按预期工作。

我找不到这方面的更多信息。有人可以确认或否认这是一个 GCC 错误吗?

是的,这是一个 GCC 错误。错误跟踪器中的相关问题是 77841。引用那边的描述:

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.

所以你问题中的new表达式应该是格式正确的,就像数组对象的直接初始化一样。

无法预测何时修复。写这篇文章时它仍然是 reproducible in GCC's trunk