- Notifications
You must be signed in to change notification settings - Fork14.5k
Closed
Description
Consider the following code:
intfoo4s() {return (0x7000'0000) <<4;}unsignedintfoo4u() {return (0x7000'0000u) <<4;}intfoo1s() {return (0x7000'0000) <<1;}unsignedintfoo1u() {return (0x7000'0000u) <<1;}
which when compiled with-std=c++20 -Wall -Wextra -Wpedantic
results in:
<source>:2:26: warning: signed shift result (0x700000000) requires 36 bits to represent, but 'int' only has 32 bits [-Wshift-overflow] return (0x7000'0000) << 4; ~~~~~~~~~~~~~ ^ ~1 warning generated.
In C++20, the semantics are just as well defined for the signed case as they are for the unsigned case, thus generating a warning when shifting bits outside of a signed target type and not when shifting outside of a unsigned target type seems inconsistent to me.
GCC does not warn in this case in C++20 mode. Seehttps://godbolt.org/z/9rbafz3h3.
This came up when discussing a similar (but not identical) issue in GCC. Seehttps://gcc.gnu.org/bugzilla/show_bug.cgi?id=103826