-fstrict-enums This option is for C++ only. ------------------ Best regards, lh_mouse 2016-05-19 ------------------------------------------------------------- 发件人:☂Josh Chia (谢任中) <joshchia@xxxxxxxxx> 发送日期:2016-05-19 09:37 收件人:gcc-help 抄送: 主题:Optimize based on possible enum class values? I have an enum class like this that I generated and examined assembly output for under gcc 5.3.1 x86_64 -O3: enum class E : uint8_t { E0, E1, E2 }; I have an 'if' condition "if (e == E::E2) ...". I see that the generated assembly involves comparing the underlying integer value against 2, and then "sete %al". If we know that the possible values are only 0, 1, 2, we can simply do a right-shift of 2 on the underlying integer value. Although I haven't done any benchmarks, this seems slightly more optimal to me. At least we save one instruction. Is there a way to tell gcc to assume that the underlying integer value is for a valid enum value and optimize accordingly? Even more generally, is there a way to give optimization hints to the compiler that a variable can only have certain values at a certain point in the code?