[Bug c++/69864] New: Narrowing conversion

mgsergio at yandex dot ru gcc-bugzilla@gcc.gnu.org
Thu Feb 18 10:37:00 GMT 2016


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

            Bug ID: 69864
           Summary: Narrowing conversion
           Product: gcc
           Version: 5.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mgsergio at yandex dot ru
  Target Milestone: ---

#include <iostream>
#include <type_traits>
using namespace std;

template <typename T, typename U>
inline T narrowing_check(U const u)
{
  // Also gives only warning
  // T t{u};
  // return t;
  return {u};
}

int main() {
  auto a = narrowing_check<int>(3.5);
  cout << a;
  // int b{2.3};
  return 0;
}

gcc emits a waring on narrowing_check although it should be generated instead.
On int b{2.3} gcc correctly rejects the code. 

g++-5 -Wall -Wextra -std=c++11 1.cc
1.cc: In instantiation of 'T narrowing_check(U) [with T = int; U = double]':
1.cc:15:36:   required from here
1.cc:11:12: warning: narrowing conversion of '(double)u' from 'double' to 'int'
inside { } [-Wnarrowing]
   return {u};

clang rejects this code.
1.cc:11:11: error: type 'double' cannot be narrowed to 'int' in initializer
list [-Wc++11-narrowing]
  return {u};
          ^
1.cc:15:12: note: in instantiation of function template specialization
'narrowing_check<int, double>' requested here
  auto a = narrowing_check<int>(3.5);
           ^
1.cc:11:11: note: insert an explicit cast to silence this issue
  return {u};
          ^
          static_cast<int>( )
1 error generated.


More information about the Gcc-bugs mailing list