40 lines
689 B
Plaintext
40 lines
689 B
Plaintext
![]() |
The attribute <code>noreturn</code> indicates that a function does not return.
|
|||
|
Using this attribute allows the compiler to do some assumptions that can lead to optimizations. However, if a function with this attribute ever returns, the behavior becomes undefined.
|
|||
|
|
|||
|
|
|||
|
== Noncompliant Code Example
|
|||
|
|
|||
|
----
|
|||
|
[[noreturn]] void f () {
|
|||
|
while (1) {
|
|||
|
// ...
|
|||
|
if (/* something*/) {
|
|||
|
return; // Noncompliant, this function should not return
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
----
|
|||
|
|
|||
|
|
|||
|
== Compliant Solution
|
|||
|
|
|||
|
----
|
|||
|
[[noreturn]] void f() { // Compliant
|
|||
|
while (true) {
|
|||
|
// ...
|
|||
|
}
|
|||
|
}
|
|||
|
----
|
|||
|
Or
|
|||
|
----
|
|||
|
void f() {
|
|||
|
while (true) {
|
|||
|
// ...
|
|||
|
if (/* something*/) {
|
|||
|
return; // Compliant
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
----
|
|||
|
|