Clang 拒绝,而 gcc 接受新的 C[ ]{1}

Ala*_*lan 7 c++

我编写了以下使用new. 但问题是 clang 拒绝该程序,而 gcc 和 msvc 接受它。我的问题是哪个编译器就在这里?

struct C{
   C(int);
};
int main() {
    C *ptr{new C[]{1}}; //clang rejects but gcc accepts
}
Run Code Online (Sandbox Code Playgroud)

居住

铿锵 说:

<source>:6:21: error: no matching constructor for initialization of 'C'
    6 |     C *ptr{new C[]{1}}; //clang rejects but gcc accepts
      |                     ^
<source>:3:4: note: candidate constructor not viable: requires 1 argument, but 0 were provided
    3 |    C(int);
      |    ^ ~~~
<source>:2:8: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided
    2 | struct C{
      |        ^
<source>:2:8: note: candidat
Run Code Online (Sandbox Code Playgroud)

use*_*570 5

这是p1009r2,程序格式良好,是一个clang bug

基本上,数组大小(当使用 with 时new现在可以从大括号 init list 中的元素数量推断出来

从新表达式中的数组大小推导

C++17 这个提议
double* p = new double[]{1,2,3};// 错误 double* p = new double[]{1,2,3};// 好的

所以 clang 在这里给出的诊断是错误的。

以下是已确认的 clang 错误报告:

Clang 拒绝有效程序new C[]{1}

  • @MarekR 是的,奇怪。以下是提交的错误:[Clang 拒绝具有新 C[\]{1} 的有效程序](https://github.com/llvm/llvm-project/issues/81157) (2认同)