Difference between revisions of "cpp/language/attributes/assume"
m (→External links: ~elink, reorder) |
m (→External links: +(optional) num=X) |
||
Line 79: | Line 79: | ||
===External links=== | ===External links=== | ||
{{elink begin}} | {{elink begin}} | ||
− | {{elink|1 | + | {{elink|num=1|Clang language extensions doc: [https://clang.llvm.org/docs/LanguageExtensions.html#langext-builtin-assume {{tt|__builtin_assume}}].}} |
− | {{elink|2 | + | {{elink|num=2|Clang attribute reference doc: [https://clang.llvm.org/docs/AttributeReference.html#assume {{tt|assume}}].}} |
− | {{elink|3 | + | {{elink|num=3|MSVC doc: [https://learn.microsoft.com/en-us/cpp/intrinsics/assume {{tt|__assume}}] built-in.}} |
− | {{elink|4 | + | {{elink|num=4|GCC does not have an assumption built-in, but using [https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005funreachable {{tt|__builtin_unreachable}}], the {{attr|assume}} can in many cases be achieved as {{c|if (!(expr)) { __builtin_unreachable(); }<!---->}}. However, this code evaluates {{c|expr}}, which could be significant if the evaluation has side effects or takes a long time. The behavior is also different if the evaluation causes an exception to be thrown, or if a function call never returns.}} |
{{elink end}} | {{elink end}} |
Revision as of 09:33, 21 January 2023
Specifies that an expression will always evaluate to true at a given point.
Contents |
Syntax
[[assume( expression )]]
|
|||||||||
expression | - | expression that must evaluate to true |
Explanation
Can only be applied to a null statement, as in [[assume(x > 0)]];. This statement is called an assumption. If the expression (contextually converted to bool) would not evaluate to true at the place the assumption occurs, the behavior is undefined. Otherwise, the statement does nothing. In particular, the expression is not evaluated (but it is still potentially evaluated).
The purpose of an assumption is to allow compiler optimizations based on the information given.
The expression may not be a comma operator expression, but enclosing the expression in parentheses will allow the comma operator to be used.
Notes
If the expression would have undefined behavior, or if it would cause an exception to be thrown, then it does not evaluate to true.
Since assumptions cause undefined behavior if they do not hold, they should be used sparingly. They are not intended as a mechanism to document the preconditions of a function or to diagnose violations of preconditions. Also, it should not be presumed, without checking, that the compiler actually makes use of any particular assumption.
Example
void f(int& x, int y) { void g(int); void h(); [[assume(x > 0)]]; // Compiler may assume x is positive g(x / 2); // More efficient code possibly generated x = 3; int z = x; [[assume((h(), x == z))]]; // Compiler may assume x would have the same value after // calling h // The assumption does not cause a call to h h(); g(x); // Compiler may replace this with g(3); h(); g(x); // Compiler may NOT replace this with g(3); // An assumption applies only at the point where it appears z = std::abs(y); [[assume((g(z), true))]]; // Compiler may assume g(z) will return g(z); // Due to above and below assumptions, compiler may replace this with g(10); [[assume(y == -10)]]; // Undefined behavior if y != -10 at this point [[assume((x - 1) * 3 == 12)]]; g(x); // Compiler may replace this with g(5); }
References
- C++23 standard (ISO/IEC 14882:2024):
- 9.12.3 Assumption attribute [dcl.attr.assume]
See also
(C++23) |
marks unreachable point of execution (function) |
External links
1. | Clang language extensions doc: __builtin_assume .
|
2. | Clang attribute reference doc: assume .
|
3. | MSVC doc: __assume built-in.
|
4. | GCC does not have an assumption built-in, but using __builtin_unreachable , the [[assume]] can in many cases be achieved as if (!(expr)) { __builtin_unreachable(); }. However, this code evaluates expr, which could be significant if the evaluation has side effects or takes a long time. The behavior is also different if the evaluation causes an exception to be thrown, or if a function call never returns.
|