Difference between revisions of "cpp/language/attributes/noreturn"
m (Clarify that noreturn refers to returning control flow (and does not refer to whether or not a function returns a non-void value)) |
Andreas Krug (Talk | contribs) m (., fmt) |
||
Line 12: | Line 12: | ||
===Explanation=== | ===Explanation=== | ||
− | Indicates that the function will not return control flow to the calling function after it finishes (e.g. functions that terminate the application, throw exceptions, loop indefinitely, etc.) | + | Indicates that the function will not return control flow to the calling function after it finishes (e.g. functions that terminate the application, throw exceptions, loop indefinitely, etc.). |
This attribute applies to the name of the function being declared in function declarations only. The behavior is undefined if the function with this attribute actually returns. | This attribute applies to the name of the function being declared in function declarations only. The behavior is undefined if the function with this attribute actually returns. | ||
Line 21: | Line 21: | ||
{{example | {{example | ||
|code= | |code= | ||
− | [[ noreturn ]] void f() { | + | [[noreturn]] void f() |
− | + | { | |
− | + | throw "error"; | |
+ | // OK | ||
} | } | ||
− | void q [[ noreturn ]] (int i) { | + | void q [[noreturn]] (int i) |
− | + | { | |
− | + | // behavior is undefined if called with an argument <= 0 | |
− | + | if (i > 0) | |
− | + | throw "positive"; | |
} | } | ||
// void h() [[noreturn]]; // error: attribute applied to function type of h, not h itself | // void h() [[noreturn]]; // error: attribute applied to function type of h, not h itself | ||
− | int main() { | + | int main() |
+ | { | ||
try { f(); } catch(...) {} | try { f(); } catch(...) {} | ||
try { q(42); } catch(...) {} | try { q(42); } catch(...) {} | ||
Line 44: | Line 46: | ||
The following standard functions are declared with {{tt|noreturn}} attribute: | The following standard functions are declared with {{tt|noreturn}} attribute: | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc h2 | Terminating functions}} | + | {{dsc h2|Terminating functions}} |
− | {{dsc inc | cpp/utility/program/dsc _Exit}} | + | {{dsc inc|cpp/utility/program/dsc _Exit}} |
− | {{dsc inc | cpp/utility/program/dsc abort}} | + | {{dsc inc|cpp/utility/program/dsc abort}} |
− | {{dsc inc | cpp/utility/program/dsc exit}} | + | {{dsc inc|cpp/utility/program/dsc exit}} |
− | {{dsc inc | cpp/utility/program/dsc quick_exit}} | + | {{dsc inc|cpp/utility/program/dsc quick_exit}} |
− | {{dsc inc | cpp/error/dsc terminate}} | + | {{dsc inc|cpp/error/dsc terminate}} |
− | {{dsc inc | cpp/error/dsc unexpected}} | + | {{dsc inc|cpp/error/dsc unexpected}} |
− | {{dsc h2 | Compiler hints}} | + | {{dsc h2|Compiler hints}} |
− | {{dsc inc | cpp/utility/dsc unreachable}} | + | {{dsc inc|cpp/utility/dsc unreachable}} |
− | {{dsc h2 | Always-throwing functions}} | + | {{dsc h2|Always-throwing functions}} |
− | {{dsc inc | cpp/error/dsc rethrow_exception}} | + | {{dsc inc|cpp/error/dsc rethrow_exception}} |
− | {{dsc mem fun | cpp/error/nested_exception/rethrow_nested | throws the stored exception}} | + | {{dsc mem fun|cpp/error/nested_exception/rethrow_nested|throws the stored exception}} |
− | {{dsc inc | cpp/error/dsc throw_with_nested}} | + | {{dsc inc|cpp/error/dsc throw_with_nested}} |
− | {{dsc h2 | Non-local jumps {{mark since c++17}} }} | + | {{dsc h2|Non-local jumps {{mark since c++17}}}} |
− | {{dsc inc | cpp/utility/program/dsc longjmp}} | + | {{dsc inc|cpp/utility/program/dsc longjmp}} |
{{dsc end}} | {{dsc end}} | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc see c | c/language/_Noreturn}} | + | {{dsc see c|c/language/_Noreturn}} |
− | {{dsc see c | c/language/attributes/noreturn | {{attr/core|noreturn}} }} | + | {{dsc see c|c/language/attributes/noreturn|{{attr/core|noreturn}}}} |
{{dsc end}} | {{dsc end}} | ||
{{langlinks|ja|zh}} | {{langlinks|ja|zh}} |
Revision as of 07:32, 20 May 2023
Indicates that the function does not return.
Contents |
Syntax
[[noreturn]]
|
|||||||||
Explanation
Indicates that the function will not return control flow to the calling function after it finishes (e.g. functions that terminate the application, throw exceptions, loop indefinitely, etc.).
This attribute applies to the name of the function being declared in function declarations only. The behavior is undefined if the function with this attribute actually returns.
The first declaration of the function must specify this attribute if any declaration specifies it. If a function is declared with [[noreturn]]
in one translation unit, and the same function is declared without [[noreturn]]
in another translation unit, the program is ill-formed; no diagnostic required.
Example
[[noreturn]] void f() { throw "error"; // OK } void q [[noreturn]] (int i) { // behavior is undefined if called with an argument <= 0 if (i > 0) throw "positive"; } // void h() [[noreturn]]; // error: attribute applied to function type of h, not h itself int main() { try { f(); } catch(...) {} try { q(42); } catch(...) {} }
Standard library
The following standard functions are declared with noreturn
attribute:
Terminating functions | |
(C++11) |
causes normal program termination without cleaning up (function) |
causes abnormal program termination (without cleaning up) (function) | |
causes normal program termination with cleaning up (function) | |
(C++11) |
causes quick program termination without completely cleaning up (function) |
function called when exception handling fails (function) | |
(deprecated in C++11)(removed in C++17) |
function called when dynamic exception specification is violated (function) |
Compiler hints | |
(C++23) |
marks unreachable point of execution (function) |
Always-throwing functions | |
(C++11) |
throws the exception from an std::exception_ptr (function) |
throws the stored exception (public member function of std::nested_exception )
| |
(C++11) |
throws its argument with std::nested_exception mixed in (function template) |
Non-local jumps (since C++17) | |
jumps to specified location (function) |
See also
C documentation for _Noreturn
| |
C documentation for
[[noreturn]] |