Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::atexit

      From cppreference.com
      <cpp‎ |utility‎ |program
       
       
      Utilities library
       
       
      Defined in header<cstdlib>
      (1)
      int atexit(/* c-atexit-handler */* func);
      int atexit(/* atexit-handler */* func);
      (until C++11)
      int atexit(/* c-atexit-handler */* func)noexcept;
      int atexit(/* atexit-handler */* func)noexcept;
      (since C++11)
      extern"C"using/* c-atexit-handler */=void();
      extern"C++"using/* atexit-handler */=void();
      (2)(exposition only*)

      Registers the function pointed to byfunc to be called on normal program termination (viastd::exit() or returning from themain function)

      The functions will be called during the destruction of the static objects, in reverse order: if A was registered before B, then the call to B is made before the call to A. Same applies to the ordering between static object constructors and the calls toatexit: seestd::exit.

      (until C++11)

      The functions may be called concurrently with the destruction of the objects with static storage duration and with each other, maintaining the guarantee that if registration of A was sequenced-before the registration of B, then the call to B is sequenced-before the call to A, same applies to the sequencing between static object constructors and calls toatexit: seestd::exit.

      (since C++11)

      The same function may be registered more than once.

      If a function exits via an exception,std::terminate is called.

      atexit is thread-safe: calling the function from several threads does not induce a data race.

      The implementation is guaranteed to support the registration of at least 32 functions. The exact limit is implementation-defined.

      Contents

      [edit]Parameters

      func - pointer to a function to be called on normal program termination

      [edit]Return value

      0 if the registration succeeds, nonzero value otherwise.

      [edit]Notes

      The two overloads are distinct because the types of the parameterfunc are distinct (language linkage is part of its type).

      [edit]Example

      Run this code
      #include <cstdlib>#include <iostream> void atexit_handler_1(){std::cout<<"At exit #1\n";} void atexit_handler_2(){std::cout<<"At exit #2\n";} int main(){constint result_1= std::atexit(atexit_handler_1);constint result_2= std::atexit(atexit_handler_2); if(result_1|| result_2){std::cerr<<"Registration failed!\n";returnEXIT_FAILURE;} std::cout<<"Returning from main...\n";returnEXIT_SUCCESS;}

      Output:

      Returning from main...At exit #2At exit #1

      [edit]See also

      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]
      registers a function to be called onstd::quick_exit invocation
      (function)[edit]
      C documentation foratexit
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/program/atexit&oldid=161407"

      [8]ページ先頭

      ©2009-2025 Movatter.jp