Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language/attributes/noreturn"

From cppreference.com
< cpp‎ | language‎ | attributes
m (use template)
m (References: Consistent order: newest to oldest)
 
(21 intermediate revisions by 8 users not shown)
Line 1: Line 1:
{{cpp/attribute/title|noreturn}}
+
{{cpp/language/attributes/title|noreturn|notes={{mark since c++11}}}}
{{cpp/attribute/navbar}}
+
{{cpp/language/attributes/navbar}}
  
 
Indicates that the function does not return.
 
Indicates that the function does not return.
Line 6: Line 6:
 
===Syntax===
 
===Syntax===
 
{{sdsc begin}}
 
{{sdsc begin}}
{{sdsc|num=1|1=
+
{{sdsc|
{{ttb|<nowiki>[[noreturn]]</nowiki>}} {{spar|function-declaration}}
+
{{ttb|{{c/core|[[noreturn]]}}}}
}}
+
{{sdsc|num=2|1=
+
{{spar|specifiers}} {{spar|function-name}} {{ttb|<nowiki>[[noreturn]]</nowiki>}} {{spar|parameters-and-qualifiers}}
+
 
}}
 
}}
 
{{sdsc end}}
 
{{sdsc end}}
  
 
===Explanation===
 
===Explanation===
Indicates that the function does not return.
+
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 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.
  
The following standard functions have this attribute: {{lc|std::_Exit}}, {{lc|std::abort}}, {{lc|std::exit}}, {{lc|std::quick_exit}}{{rev inl|since=c++17|, {{lc|std::longjmp}}}}, {{lc|std::unexpected}}, {{lc|std::terminate}}, {{lc|std::rethrow_exception}}, {{lc|std::throw_with_nested}}, {{lc|std::nested_exception::rethrow_nested}}.
+
The first declaration of the function must specify this attribute if any declaration specifies it. If a function is declared with {{attr|noreturn}} in one translation unit, and the same function is declared without {{attr|noreturn}} in another translation unit, the program is ill-formed; no diagnostic required.
  
 
===Example===
 
===Example===
 
{{example
 
{{example
 
|code=
 
|code=
[[ noreturn ]] void f() {
+
[[noreturn]] void f()
  throw "error";
+
{
  // OK
+
    throw "error";
 +
    // OK
 
}
 
}
  
[[ noreturn ]] void q(int i) {
+
void q [[noreturn]] (int i)
  // behavior is undefined if called with an argument <= 0
+
{
  if (i > 0) {
+
    // behavior is undefined if called with an argument <= 0
    throw "positive";
+
    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 {{tt|noreturn}} attribute:
 +
{{dsc begin}}
 +
{{dsc h2|Terminating functions}}
 +
{{dsc inc|cpp/utility/program/dsc _Exit}}
 +
{{dsc inc|cpp/utility/program/dsc abort}}
 +
{{dsc inc|cpp/utility/program/dsc exit}}
 +
{{dsc inc|cpp/utility/program/dsc quick_exit}}
 +
{{dsc inc|cpp/error/dsc terminate}}
 +
{{dsc inc|cpp/error/dsc unexpected}}
 +
{{dsc h2|Compiler hints}}
 +
{{dsc inc|cpp/utility/dsc unreachable}}
 +
{{dsc h2|Always-throwing functions}}
 +
{{dsc inc|cpp/error/dsc rethrow_exception}}
 +
{{dsc mem fun|cpp/error/nested_exception/rethrow_nested|throws the stored exception}}
 +
{{dsc inc|cpp/error/dsc throw_with_nested}}
 +
{{dsc h2|Non-local jumps {{mark since c++17}}}}
 +
{{dsc inc|cpp/utility/program/dsc longjmp}}
 +
{{dsc end}}
 +
 +
===References===
 +
{{ref std c++23}}
 +
{{ref std|section=9.12.10|title=Noreturn attribute|id=dcl.attr.noreturn}}
 +
{{ref std end}}
 +
{{ref std c++20}}
 +
{{ref std|section=9.12.9|title=Noreturn attribute|id=dcl.attr.noreturn}}
 +
{{ref std end}}
 +
{{ref std c++17}}
 +
{{ref std|section=10.6.8|title=Noreturn attribute|id=dcl.attr.noreturn}}
 +
{{ref std end}}
 +
{{ref std c++14}}
 +
{{ref std|section=7.6.3|title=Noreturn attribute|id=dcl.attr.noreturn}}
 +
{{ref std end}}
 +
{{ref std c++11}}
 +
{{ref std|section=7.6.3|title=Noreturn attribute|id=dcl.attr.noreturn}}
 +
{{ref std end}}
 +
 +
===See also===
 +
{{dsc begin}}
 +
{{dsc see c|c/language/_Noreturn}}
 +
{{dsc see c|c/language/attributes/noreturn|{{attr/core|noreturn}}}}
 +
{{dsc end}}
  
 
{{langlinks|ja|zh}}
 
{{langlinks|ja|zh}}

Latest revision as of 11:45, 12 August 2024

 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 
 
Attributes
(C++23)
(C++14)
(C++20)
(C++17)
noreturn
(C++11)
(C++20)
 

Indicates that the function does not return.

Contents

[edit] Syntax

[[noreturn]]

[edit] 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.

[edit] 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(...) {}
}

[edit] Standard library

The following standard functions are declared with noreturn attribute:

Terminating functions
(C++11)
causes normal program termination without cleaning up
(function) [edit]
causes abnormal program termination (without cleaning up)
(function) [edit]
causes normal program termination with cleaning up
(function) [edit]
causes quick program termination without completely cleaning up
(function) [edit]
function called when exception handling fails
(function) [edit]
(deprecated in C++11)(removed in C++17)
function called when dynamic exception specification is violated
(function) [edit]
Compiler hints
marks unreachable point of execution
(function) [edit]
Always-throwing functions
throws the exception from an std::exception_ptr
(function) [edit]
throws the stored exception
(public member function of std::nested_exception)
throws its argument with std::nested_exception mixed in
(function template) [edit]
Non-local jumps (since C++17)
jumps to specified location
(function) [edit]

[edit] References

  • C++23 standard (ISO/IEC 14882:2024):
  • 9.12.10 Noreturn attribute [dcl.attr.noreturn]
  • C++20 standard (ISO/IEC 14882:2020):
  • 9.12.9 Noreturn attribute [dcl.attr.noreturn]
  • C++17 standard (ISO/IEC 14882:2017):
  • 10.6.8 Noreturn attribute [dcl.attr.noreturn]
  • C++14 standard (ISO/IEC 14882:2014):
  • 7.6.3 Noreturn attribute [dcl.attr.noreturn]
  • C++11 standard (ISO/IEC 14882:2011):
  • 7.6.3 Noreturn attribute [dcl.attr.noreturn]

[edit] See also

C documentation for _Noreturn
C documentation for [[noreturn]]