""Why is this UB not caught during constexpr execution? #include <cstdint> consteval…
- User
""Why is this UB not caught during constexpr execution?
#include
consteval std::int8_t f(std::int8_t value) { for(int i = 0; i < 1000; ++i) { value -= 1; } return value; }
static_assert(f(0) == 24);""
- Assistant
Short answer: there isn’t undefined behavior happening here in a way a constexpr checker would catch. The only potential “overflow” is a narrowing conversion from int to int8_t at the end of each loop iteration, which the C++ standard treats as implementation-defined (not UB). In practice, on common 8-bit two’s-complement platforms, the repeated subtraction ends up modulo 2^8, so after 1000 steps you get -1000 mod 256 = 24, which is what your static_assert checks.
Details
-
What actually happens in value -= 1
- value is int8_t. In value -= 1, the operands are promoted to int, the subtraction is performed as int, and then the result is assigned back to int8_t.
- With 1000 iterations starting from 0, the final value computed before narrowing is -1000 (an int, which is safely within usual int ranges).
- The assignment to int8_t is a narrowing conversion. If -1000 cannot be represented in int8_t (which is the case), the result is implementation-defined. It is not required to be undefined behavior.
-
Why the static_assert ends up as 24
- On most platforms with 8-bit int8_t and wrap-around behavior, converting -1000 to int8_t yields 24 (because -1000 mod 256 = 24). That’s why f(0) evaluates to 24 and the static_assert passes.
- This is not portable: some exotic or non-two’s-complement representations could yield a different value for the same code.
-
So why isn’t this caught as UB by constexpr evaluation?
- Because there isn’t UB in the standard sense. The only potentially problematic step is the narrowing conversion, which is described as implementation-defined, not undefined behavior.
- constexpr evaluation follows the implementation’s rules for such conversions. If you want a portable, well-defined behavior, don’t rely on a narrowing conversion from int to int8_t after a large negative value.
What you can do if you want predictable behavior
- Use unsigned types if you want well-defined modulo wrap-around:
- Example: use uint8_t for the parameter and the return, or perform the arithmetic in int and then wrap explicitly with modulo if that’s the intent.
- If you want to detect overflow at compile time (portable UB detection isn’t guaranteed by the standard), you’d typically need a different approach or compiler-specific flags (not standardized).
A quick illustrative variant
- If you want to avoid the portability issue, you can use unsigned arithmetic, which has well-defined wrap-around:
- consteval std::uint8_t f(std::uint8_t value) { for (int i = 0; i < 1000; ++i) { value -= 1; } return value; }
- static_assert(f(0) == 24);
Bottom line: your code isn’t “catching UB” at constexpr time because there isn’t UB to catch; the behavior is implementation-defined due to the narrowing conversion, and on typical platforms it ends up as 24, which makes the static_assert pass. If you need portable behavior, avoid relying on narrowing conversions to storage-width types in constexpr code.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.