为什么 clang 在类似问题上进行不同的诊断?

Why different diagnostics by clang on similar issue?

这是我的代码:

#include <vector>

int main()
{
    std::vector<int> a = {1, 2.2};
}

正如预期的那样,由于非法的缩小转换而无法编译:

$ clang++ -std=c++11 foo.cpp
foo.cpp:5:30: error: type 'double' cannot be narrowed to 'int' in initializer list [-Wc++11-narrowing]
    std::vector<int> a = {1, 2.2};
                             ^~~

为什么编译后只有一个警告?

#include <complex>

int main()
{
    std::complex<int> a = {1, 2.2};
}

这是警告:

$ clang++ -std=c++11 foo.cpp 
foo.cpp:5:31: warning: implicit conversion from 'double' to 'std::__1::complex<int>::value_type' (aka 'int') changes value from 2.2 to 2 [-Wliteral-conversion]
    std::complex<int> a = {1, 2.2};
                          ~   ^~~
1 warning generated.

我理解诊断的概念,C++ 编译器只需要以错误或警告的形式发出诊断。

但我想确定我没有误解这里的任何概念。特别是,我想知道是否有任何 C++ 概念我需要知道为什么在第一种情况下发出 c++11-narrowing 诊断但在第二种情况下发出 literal-conversion 诊断。

两种不同的情况正在发生,如您所见,这会导致不同的诊断:

  • 在第一种情况下,您正在使用 copy-list-initialization 并找到 std::vector 的构造函数,它采用 std::initializer_lsit.

  • 在第二种情况下,您在 LiteralType 上使用 copy-list-initialization,结果是 聚合初始化.

它们都应该防止缩小转换,但 clang 似乎以不同的方式实现诊断。