Warning : overflow in implicit constant conversion

前端 未结 4 2086
醉话见心
醉话见心 2021-01-01 17:30

In the following program, the line 5 does give overflow warning as expected, but surprisingly the line 4 doesn\'t give any warning in GCC: http://www.ideone.com/U0

4条回答
  •  抹茶落季
    2021-01-01 18:15

    Post GCC 4.3, the semantics of -Wconversion have been updated to detect implicit conversions that might change a value, but you have to enable -Wsign-conversion as well, because otherwise you won't get a warning for code that might change the sign of a number due to coercion between signed and unsigned types.

    Contrary to what Crazy Eddie is saying, prior to GCC 4.3 (which hadn't been released yet at the time) -Wconversion didn't generically check for problems introduced by implicit type conversion and the like. Rather, it checked whether your program would behave differently than it would have behaved if it had used old-style K&R function prototypes.

    This not only meant that it didn't give a warning on all implicit type conversion / coercion problems, but it also meant that some good code gave an unnecessary warning. And of course, you'd get no error with g++ because such prototypes aren't valid C++ anyway.

提交回复
热议问题