https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102379

            Bug ID: 102379
           Summary: missing -Wnarrowing even in instantiated template
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

Prompted by pr102378 (and looking for examples where G++ does issue diagnostics
for template code): G++ fails to diagnose the following invalid code, in both
the uninstantiated template and in the explicitly instantiated one.

$ cat t.C && gcc -O2 -S -Wall -std=c++11 -Wnarrowing t.C
template <class T>
int f ()
{
  int i = { 2.2 };   // missing -Wnarrowing
  return i;
}

template <class T>
int g ()
{
  int i = { 2.2 };   // missing -Wnarrowing despite explicit instantiation
  return i;
}

template <int> int g ();


Clang diagnoses both:

$ clang -S -Wall t.C
t.C:4:13: error: type 'double' cannot be narrowed to 'int' in initializer list
      [-Wc++11-narrowing]
  int i = { 2.2 };   // missing -Wnarrowing
            ^~~
t.C:4:13: note: insert an explicit cast to silence this issue
  int i = { 2.2 };   // missing -Wnarrowing
            ^~~
            static_cast<int>( )
t.C:4:13: warning: implicit conversion from 'double' to 'int' changes value
from
      2.2 to 2 [-Wliteral-conversion]
  int i = { 2.2 };   // missing -Wnarrowing
          ~ ^~~
t.C:11:13: error: type 'double' cannot be narrowed to 'int' in initializer list
      [-Wc++11-narrowing]
  int i = { 2.2 };   // missing -Wnarrowing despite explicit instantiation
            ^~~
t.C:11:13: note: insert an explicit cast to silence this issue
  int i = { 2.2 };   // missing -Wnarrowing despite explicit instantiation
            ^~~
            static_cast<int>( )
t.C:11:13: warning: implicit conversion from 'double' to 'int' changes value
      from 2.2 to 2 [-Wliteral-conversion]
  int i = { 2.2 };   // missing -Wnarrowing despite explicit instantiation
          ~ ^~~
2 warnings and 2 errors generated.

Reply via email to