http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51151
Bug #: 51151 Summary: Invalid -Woverflow warning in C++ frontend Classification: Unclassified Product: gcc Version: 4.7.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassig...@gcc.gnu.org ReportedBy: i...@airs.com Compile this C++ file with -Wall or -Woverflow: template<typename T> class C { public: void f() { m = c2 + 1; } static const long unsigned int c1 = 7; static const int c2 = c1 - 1; int m; }; template class C<int>; When I compile this with current mainline, I get: foo.cc: In instantiation of ‘void C<T>::f() [with T = int]’: foo.cc:10:16: required from here foo.cc:4:5: warning: integer overflow in expression [-Woverflow] This warning is invalid. There is no integer overflow here. From a quick look, it appears that fold winds up being called with ((int) ((unsigned int) 7 + 4294967295U)) + 1 It decides to distribute the addition, groups the constants 4294967295 and 1 together, and gets an overflow when it adds them. At that point it is adding together an unsigned and a signed constant, having decided to strip the type conversion as a nop. This does not happen when not using a template.