With -O2, this testcase aborts on x86_64: extern "C" void abort ();
enum E { A, B, C, D }; void CheckE(const E value) { long v = value; if (v <= D) abort (); } int main() { CheckE(static_cast<E>(5)); } Presumably something in the middle- or back-end is believing what the C++ front end sets TYPE_MAX_VALUE to and optimizing based on the assumption that "value" cannot be greater than 3. I think the C++ standard can definitely be read to allow this optimization, but it means optimizing away bounds checking, which is a serious security problem. The standard should be fixed. -- Summary: G++ is too aggressive in optimizing away bounds checking with enums Product: gcc Version: 4.5.0 Status: UNCONFIRMED Keywords: wrong-code Severity: normal Priority: P3 Component: c++ AssignedTo: jason at gcc dot gnu dot org ReportedBy: jason at gcc dot gnu dot org GCC target triplet: x86_64-unknown-linux-gnu http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43680