std::abort
Min standard notice:
Header: <cstdlib>
Causes abnormal program termination unless SIGABRT is being caught by a signal handler passed to std::signal and the handler does not return.
# Declarations
void abort();
(until C++11)
[[noreturn]] void abort() noexcept;
(since C++11)
# Return value
None because it does not return.
# Notes
POSIX specifies that the abort() function overrides blocking or ignoring the SIGABRT signal.
Some compiler intrinsics, e.g. __builtin_trap (gcc, clang, and icc) or __fastfail/__debugbreak (msvc), can be used to terminate the program as fast as possible.
# Example
#include <csignal>
#include <cstdlib>
#include <iostream>
class Tester
{
public:
Tester() { std::cout << "Tester ctor\n"; }
~Tester() { std::cout << "Tester dtor\n"; }
};
Tester static_tester; // Destructor not called
void signal_handler(int signal)
{
if (signal == SIGABRT)
std::cerr << "SIGABRT received\n";
else
std::cerr << "Unexpected signal " << signal << " received\n";
std::_Exit(EXIT_FAILURE);
}
int main()
{
Tester automatic_tester; // Destructor not called
// Setup handler
auto previous_handler = std::signal(SIGABRT, signal_handler);
if (previous_handler == SIG_ERR)
{
std::cerr << "Setup failed\n";
return EXIT_FAILURE;
}
std::abort(); // Raise SIGABRT
std::cout << "This code is unreachable\n";
}