In C++23, the [[assume(expression)]]
attribute makes it so that if expression is false
, the behavior is undefined.
For example:
int div(int x, int y) {
[[assume(y == 1)]];
return x / y;
}
This compiles to the same code as if y
was always 1
.
div(int, int):
mov eax, edi
ret
However, what happens if there is another level of undefined behavior?
int div(int x, int y) {
[[assume(x / 0 == 1)]];
return x / y;
}
Now there is UB inside the assumption, but the assumption is not evaluated. What does this mean? Is it just nonsense or can the compiler do anything with this assumption?