What contract does the C++23 [[assume]] attribute make, and why is a false assumption undefined behavior?
Show answer and explanation
C++23 [[assume(expression)]] applies to a null statement and tells the implementation it may optimize as though the expression would be true at that point. The expression is not evaluated, so side effects inside it do not happen; if it would evaluate false, evaluating the assumption has runtime undefined behavior. An implementation may also choose not to exploit the information. It is therefore not validation, error handling, or a debug assertion: validate untrusted input first, use assertions for diagnosable invariants, and reserve assume for a proven hot-path fact whose violation is already impossible under the program contract.
C++23 [[assume(expression)]] applies to a null statement and tells the implementation it may optimize as though the expression would be true at that point. The expression is not evaluated, so side effects inside it do not happen; if it would evaluate false, evaluating the assumption has runtime undefined behavior. An implementation may also choose not to exploit the information. It is therefore not validation, error handling, or a debug assertion: validate untrusted input first, use assertions for diagnosable invariants, and reserve assume for a proven hot-path fact whose violation is already impossible under the program contract.
Connect the conclusion directly to the type, lifetime, ownership, overload-resolution rule, or library contract present in the question. State implementation dependence or a violated precondition explicitly instead of merely guessing output.