Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      C++ attribute: noreturn(since C++11)

      From cppreference.com
      <cpp‎ |language‎ |attributes
       
       
      C++ language
      General topics
      Flow control
      Conditional execution statements
      Iteration statements (loops)
      Jump statements
      Functions
      Function declaration
      Lambda function expression
      inline specifier
      Dynamic exception specifications(until C++17*)
      noexcept specifier(C++11)
      Exceptions
      Namespaces
      Types
      Specifiers
      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
      Special member functions
      Templates
      Miscellaneous
       
      Declarations
       
      Attributes
      (C++23)
      (C++11)(until C++26)
      (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.

      If a function previously declared with[[noreturn]] is invoked and that invocation eventually returns, the behavior isruntime-undefined.

      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

      Run this code
      [[noreturn]]void f(){throw"error";// OK} void q[[noreturn]](int i){// behavior is undefined if called with an argument <= 0if(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 withnoreturn 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]
      (C++11)
      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 anstd::exception_ptr
      (function)[edit]
      throws the stored exception
      (public member function ofstd::nested_exception)
      throws its argument withstd::nested_exception mixed in
      (function template)[edit]
      Non-local jumps(since C++17)
      jumps to specified location
      (function)[edit]

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      CWG 2924C++11returning from a[[noreturn]] function
      would result in undefined behavior
      results in runtime-
      undefined behavior

      [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]]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/attributes/noreturn&oldid=180151"

      [8]ページ先頭

      ©2009-2025 Movatter.jp