§The Annotated C++ StandardC++ 标准注疏 中文
9.13.3

Assumption attribute

[[assume(expr)]]; tells the compiler expr is true here, for use in optimization: the expression is never executed, but if the assumption doesn't hold the behavior is undefined; the compiler may also ignore it entirely.

Key points

  • [[assume(e)]]; is the programmer's guarantee to the compiler: e is true when execution reaches this point. The compiler can use it to remove redundant checks and branches, or ignore it entirely.
  • e is never executed; [[assume(++y == 43)]] doesn't increment y. But e is still potentially evaluated, so variables it uses must be captured by lambdas.
  • If e wouldn't evaluate to true here, executing the assumption is undefined behavior. A wrong assumption can make the compiler delete your safety-net checks.
  • Whether a false assumption during constant evaluation is an error is up to the implementation; Clang reports one.
  • Clang ignores assumptions with side effects (including calls to ordinary functions), with a -Wassume warning.

What an assumption buys you#

C++✓ well-formedgodbolt ↗
int divide_by_32(int x) {
  [[assume(x >= 0)]];
  return x / 32;
}
C++✓ well-formedgodbolt ↗
int plain(int x) {
  return x / 32;
}

Code generated by Clang 23 with -O2 for x86-64:

divide_by_32:                plain:
    mov  eax, edi                lea   eax, [rdi + 31]
    shr  eax, 5                  test  edi, edi
    ret                          cmovns eax, edi
                                 sar   eax, 5
                                 ret

Signed integer division rounds toward zero. Right-shifting a negative number rounds toward negative infinity, so plain has to add 31 to negative numbers before the arithmetic right shift. divide_by_32 has told the compiler x >= 0, so one logical right shift is enough.

[[assume(e)]]; is the assumption (assumption) introduced in C++23; P1774R8 standardized built-ins that vendors already had: Clang's __builtin_assume, MSVC's __assume, and the if (!(e)) __builtin_unreachable(); commonly used with GCC.

The rule (¶1)#

The standard says

If the converted expression would evaluate to true at the point where the assumption appears, the assumption has no effect. Otherwise, evaluation of the assumption has runtime-undefined behavior.

The rules of ¶1 come down to four points:

  1. assume can only be applied to a null statement, i.e. a lone ;. Such a statement is called an assumption.
  2. It must have an argument of the form ( conditional-expression ), contextually converted to bool ([conv.general]).
  3. The expression is not evaluated.
  4. If the converted expression would evaluate to true at the point where the assumption appears, the assumption has no effect; otherwise, evaluating the assumption has runtime-undefined behavior (runtime-undefined behavior): UB in ordinary execution, with separate rules for constant evaluation (see below).
C++✗ ill-formedgodbolt ↗
int g(int x) {
  [[assume(x > 0), assume(x < 10)]];  // OK: two assumptions
  [[assume(x > 0 && x < 10)]];        // OK
  [[assume(x > 0, x < 10)]];          // error: top-level comma
  return x;
}
int h(int x) {
  [[assume(x > 0)]] return x;         // error: only for null statements
}
[[assume(true)]] int bad;             // error: can't be applied to a declaration

Restricting the argument to a conditional-expression excludes assignment and comma expressions. [[assume(x = 1)]] is almost certainly == mistyped as =; a top-level comma is easily misread as two arguments. If you really want them, add parentheses, as in [[assume((x > 0, x < 10))]], but that is a comma expression and only assumes x < 10.

The expression isn't executed; what counts is the hypothetical result#

C++✓ well-formedgodbolt ↗
int f(int y) {
  [[assume(++y == 43)]];   // y is not incremented
  return y;                // can be replaced by return 42;
}
int main() { return f(42) - 42; }

This is the standard's example. The rule says "would evaluate to true": what matters is what the result would be if it were evaluated once here. In f(42), hypothetically evaluating ++y == 43 gives true, so there's no UB, and y isn't actually incremented. The compiler deduces that y equals 42 on entry and can replace return y; with return 42;. When f(41) is called, the hypothetical result is false, so it's UB.

By this reading, the following also don't count as evaluating to true, and are likewise UB:

  • the hypothetical evaluation itself has UB, such as [[assume(*p == 0)]] when p is null;
  • the hypothetical evaluation would throw an exception, not terminate, or call std::exit.

Clang 23 actually ignores this assumption because the expression has side effects; see below.

Potentially evaluated: it odr-uses (¶2)#

C++✗ ill-formedgodbolt ↗
void f(int x) {
  auto l1 = [] { [[assume(x > 0)]]; };   // error: x not captured
  auto l2 = [=] { [[assume(x > 0)]]; };  // OK: x captured by value
  static_assert(sizeof(l2) == sizeof(int));
  (void)l1;
}

¶2 Note 1 points out that the expression of an assumption is potentially evaluated ([basic.def.odr]). It differs from the operands of sizeof and decltype, which are unevaluated operands. So variables appearing in the expression are odr-used, functions used must be defined, and in a lambda they must be captured. That's why l2 above gets an extra member of int size.

The reason is that the meaning of an assumption rests on the result of a hypothetical evaluation. For that result to be well-defined, the expression has to go through name binding, template instantiation and odr-use completely, like any ordinary expression.

The compiler may ignore it (¶2)#

The second half of Note 1 in ¶2 explains the design intent: implementations can analyze the form of the expression to deduce information for optimization, but are not required to deduce anything from any assumption. An implementation that makes no use of assumptions should report 0 for __has_cpp_attribute(assume).

So an assumption is a one-sided promise: the programmer guarantees e is true, and failing that it's UB; the compiler may or may not use it. Removing an assumption only turns formerly-UB executions into defined ones, which is exactly the ignorability [dcl.attr.grammar]/8 requires of standard attributes.

Assumptions with side effects#

C++✓ well-formedgodbolt ↗
int counter();
void f() {
  [[assume(counter() > 0)]];   // valid, but ignored by Clang
}
int main() {}

The standard allows the expression to have side effects, since it isn't executed anyway. But it's hard for a compiler to deduce useful facts from such an expression. Clang 23 ignores the whole assumption as soon as it finds the expression might have side effects, including calling a function not marked const or pure, and warns assumption is ignored because it contains (potential) side-effects. Since implementations needn't deduce anything, this is allowed.

Assumptions during constant evaluation#

C++? implementation-definedgodbolt ↗
constexpr int g(int x) {
  [[assume(x > 0)]];
  return x;
}
constexpr int a = g(1);    // OK
constexpr int b = g(-1);   // whether this is an error is up to the implementation

Undefined behavior is normally not allowed during constant evaluation, and encountering it makes the expression not a constant expression. Runtime-undefined behavior is an exception: per [expr.const.core]/3, if an expression could fail to be a constant expression only because of it, the result is implementation-defined. So when constant evaluation meets a false assumption, an implementation may report an error or ignore it and carry on. Clang 23 chooses to report an error, assumption evaluated to false.

It isn't required to always be an error because that would force implementations to actually evaluate every assumption expression during constant evaluation. Assumption expressions may be expensive or have side effects, such as ++y == 43, and by design shouldn't be evaluated.

Comparison with similar tools#

Form When the condition is false Executes the expression Purpose
[[assume(e)]]; Runtime UB No Give the optimizer a fact
assert(e) Debug builds terminate; does nothing with NDEBUG defined Only in debug builds Debug checking
contract_assert(e) (C++26, [basic.contract]) Depends on the evaluation semantic: ignore, report and continue, terminate Depends on the semantic Checking and documentation
if (!e) std::unreachable(); (C++23) UB Yes Mark unreachable code

The std::unreachable() form executes e, so any side effects happen; assume doesn't.

A common practice is a macro that checks the condition in debug builds and becomes an assumption in release builds. This turns an assertion failure in release builds from a reportable error into arbitrary behavior; understand that cost before using it.

Implementation notes

  • Clang lowers [[assume(e)]] to LLVM's llvm.assume, the same as __builtin_assume(e).
  • GCC supports [[assume]] since GCC 13 and also accepts __attribute__((assume(e))).
  • MSVC has long provided __assume(e), with __assume(0) meaning unreachable.
  • Assumptions leave extra nodes in the optimizer's intermediate representation, and too many or too complex assumptions can hinder other optimizations. Use them only when measurement shows a benefit.

History#

History

  • Introduced in C++23 (P1774R8). The proposal discussed several forms, including a function, a keyword and an attribute, and settled on an attribute because removing it doesn't change the meaning of a correct program.
  • The C++23 wording said the behavior is undefined and listed a false assumption during constant evaluation as a case where it is unspecified whether the expression is a core constant expression. The current draft introduced the term runtime-undefined behavior and made it implementation-defined.

Common misconceptions#

A wrong assumption just means the optimization doesn't happen

A wrong assumption is UB. The compiler can delete code based on the assumption, including your safety-net checks:

C++
int f(int* p) {
  [[assume(p != nullptr)]];
  if (!p) return -1;   // this line can simply be deleted
  return *p;
}

The expression is executed

[[assume(init() == 0)]]; doesn't call init().

Questions#

Question 1

Does f(41) below have UB? What does f(42) return?

C++
int f(int y) {
  [[assume(++y == 43)]];
  return y;
}

Answer

f(41): in the hypothetical evaluation y becomes 42 and the comparison is false, so the assumption wouldn't evaluate to true; executing it is UB, and the return value has no guarantees.

f(42): the assumption holds, so there's no UB. The expression isn't actually executed, y is still 42, and it returns 42.

Question 2

Why does [[assume]] satisfy the ignorability requirement of [dcl.attr.grammar]/8? Imagine an attribute that would violate it.

Answer

A program with [[assume(e)]] has UB in executions where e is false, allowing any behavior. After removing it, those executions continue by the ordinary rules, with results that fall within "any behavior"; executions where e is true are unaffected. So the possible executions after removal are a subset of the original ones.

A hypothetical attribute that violates it is [[check(e)]], meaning call std::terminate if e is false. Removing it, executions that were certain to terminate would carry on running, executions impossible in the original program. This is why contract assertions need their own syntax and can't be attributes.

The authoritative text is eel.is/c++draft/dcl.attr.assume (working draft 2026-08-23, commit c7015b485c). These are study notes that explain rather than quote; ¶ numbers follow the draft. Translated from the Chinese original. Last updated 2026-10-19.