The complement of true is true, except when it's false
2 days ago
- P4313R1 proposes adding bitmask operations for enums using a C++26 annotation, but has potential footguns.
- Integral promotion: types smaller than int are promoted to int for bitwise ops, which can cause unexpected behavior and is unavoidable.
- Bitwise complement of bool always yields true due to boolean conversion; however, GCC has a long-standing bug where it truncates to low bit instead.
- Enums with bool as underlying type lead to confusion; GCC truncates to the lowest bit, while Clang/MSVC follow standard boolean conversion.
- Unscoped enums without a fixed underlying type have a limited valid value range; casting out-of-range values (e.g., from operator~) is undefined behavior.
- User-defined bitmask operators should constrain against bool underlying types and unfixed enums to avoid UB; a trait using E{0} can detect unfixed enums.
- Floating point to bool-backed enum conversion is inconsistent across compilers (GCC/Clang truncate, MSVC does boolean conversion), leading to potential UB.
- P4313 should add constraints (e.g., on scoped enum or underlying type not bool) to prevent these issues; the author intends to contact the paper's authors.