我编写了以下使用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)
这是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 错误报告:
| 归档时间: |
|
| 查看次数: |
125 次 |
| 最近记录: |