Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/utility/program/quick exit"

From cppreference.com
< cpp‎ | utility‎ | program
m (Example: +)
m (fmt, .)
 
Line 1: Line 1:
{{cpp/title| quick_exit}}
+
{{cpp/title|quick_exit}}
 
{{cpp/utility/program/navbar}}
 
{{cpp/utility/program/navbar}}
{{ddcl | header=cstdlib | since=c++11 |
+
{{ddcl|header=cstdlib|since=c++11|
 
[[noreturn]] void quick_exit( int exit_code ) noexcept;
 
[[noreturn]] void quick_exit( int exit_code ) noexcept;
 
}}
 
}}
Line 7: Line 7:
 
Causes normal program termination to occur without completely cleaning the resources.
 
Causes normal program termination to occur without completely cleaning the resources.
  
Functions passed to {{lc|std::at_quick_exit}} are called in reverse order of their registration. If an exception tries to propagate out of any of the functions, {{lc|std::terminate}} is called. After calling the registered functions, calls {{c|std::_Exit(exit_code)}}
+
Functions passed to {{lc|std::at_quick_exit}} are called in reverse order of their registration. If an exception tries to propagate out of any of the functions, {{lc|std::terminate}} is called. After calling the registered functions, calls {{c|std::_Exit(exit_code)}}.
  
 
Functions passed to {{lc|std::atexit}} are not called.
 
Functions passed to {{lc|std::atexit}} are not called.
 +
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | exit_code | exit status of the program}}
+
{{par|exit_code|exit status of the program}}
 
{{par end}}
 
{{par end}}
  
Line 20: Line 21:
 
===Example===
 
===Example===
 
{{example
 
{{example
|
+
|code=
| code=
+
 
#include <cstdlib>
 
#include <cstdlib>
 
#include <iostream>
 
#include <iostream>
  
template <int N>
+
template<int N>
 
void quick_exit_handler()
 
void quick_exit_handler()
 
{
 
{
Line 38: Line 38:
 
int main()
 
int main()
 
{
 
{
     if ( std::at_quick_exit( quick_exit_handler<1> ) or
+
     if (std::at_quick_exit(quick_exit_handler<1>) {{!!}}
        std::at_quick_exit( quick_exit_handler<2> ) )
+
        std::at_quick_exit(quick_exit_handler<2>))
 
     {
 
     {
 
         std::cerr << "Registration failed\n";
 
         std::cerr << "Registration failed\n";
Line 45: Line 45:
 
     }
 
     }
  
     std::atexit( at_exit_handler ); // the handler will not be called
+
     std::atexit(at_exit_handler); // the handler will not be called
  
 
     struct R { ~R() { std::cout << "destructor\n"; } } resource;
 
     struct R { ~R() { std::cout << "destructor\n"; } } resource;
Line 51: Line 51:
 
     /*...*/
 
     /*...*/
  
     std::quick_exit( EXIT_SUCCESS );
+
     std::quick_exit(EXIT_SUCCESS);
  
 
     std::cout << "This statement is unreachable...\n";
 
     std::cout << "This statement is unreachable...\n";
 
}
 
}
| output=
+
|output=
 
quick_exit handler #2
 
quick_exit handler #2
 
quick_exit handler #1
 
quick_exit handler #1
Line 62: Line 62:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{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 atexit}}
+
{{dsc inc|cpp/utility/program/dsc atexit}}
{{dsc inc | cpp/utility/program/dsc at_quick_exit}}
+
{{dsc inc|cpp/utility/program/dsc at_quick_exit}}
{{dsc see c | c/program/quick_exit}}
+
{{dsc see c|c/program/quick_exit}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 21:47, 25 October 2023

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
Defined in header <cstdlib>
[[noreturn]] void quick_exit( int exit_code ) noexcept;
(since C++11)

Causes normal program termination to occur without completely cleaning the resources.

Functions passed to std::at_quick_exit are called in reverse order of their registration. If an exception tries to propagate out of any of the functions, std::terminate is called. After calling the registered functions, calls std::_Exit(exit_code).

Functions passed to std::atexit are not called.

Contents

[edit] Parameters

exit_code - exit status of the program

[edit] Return value

(none)

[edit] Example

#include <cstdlib>
#include <iostream>
 
template<int N>
void quick_exit_handler()
{
    std::cout << "quick_exit handler #" << N << std::endl; // flush is intended
}
 
void at_exit_handler()
{
    std::cout << "at_exit handler\n";
}
 
int main()
{
    if (std::at_quick_exit(quick_exit_handler<1>) ||
        std::at_quick_exit(quick_exit_handler<2>))
    {
        std::cerr << "Registration failed\n";
        return EXIT_FAILURE;
    }
 
    std::atexit(at_exit_handler); // the handler will not be called
 
    struct R { ~R() { std::cout << "destructor\n"; } } resource;
 
    /*...*/
 
    std::quick_exit(EXIT_SUCCESS);
 
    std::cout << "This statement is unreachable...\n";
}

Output:

quick_exit handler #2
quick_exit handler #1

[edit] See also

causes abnormal program termination (without cleaning up)
(function) [edit]
causes normal program termination with cleaning up
(function) [edit]
registers a function to be called on std::exit() invocation
(function) [edit]
registers a function to be called on std::quick_exit invocation
(function) [edit]
C documentation for quick_exit