跨编译器的不一致诊断以缩小非类型模板参数中的转换

inconsistent diagnostics across compilers for narrowing conversions in non-type template argument

template <bool>
void f() { }

int main ()
{
    constexpr long long int num = 5;
    f<num & 4>(); // bitwise & is intended
    return 0;
}

gcc 9 有问题:

error: no matching function for call to 'f<(5 & 4)>()'
error: narrowing conversion of '4' from 'long long int' to 'bool' [-Wnarrowing]

clang 有问题:

error: no matching function for call to 'f'

gcc 8 和 msvc 编译没有错误。

谁是正确的?我认为错误是正确的,但想确认一下。

这是 ill-formed 因为 template non-type argument,

The template argument that can be used with a non-type template parameter can be any converted constant expression of the type of the template parameter.

并且窄转换不被视为converted constant expression

A converted constant expression of type T is an expression implicitly converted to type T, where the converted expression is a constant expression, and the implicit conversion sequence contains only:

  • ...
  • non-narrowing integral conversions
  • ...