Yashas
Yashas

Reputation: 1234

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 has a problem:

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

clang has a problem:

error: no matching function for call to 'f'

gcc 8 and msvc compile without errors.

Who is correct? I think the error is correct but wanted to make sure.

Upvotes: 2

Views: 60

Answers (1)

songyuanyao
songyuanyao

Reputation: 172934

This is ill-formed because for 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.

And the narrow conversion is not considered as 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
  • ...

Upvotes: 4

Related Questions