Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no std::on_exit?

A program can exit with a variety of different status codes.
I'd like to bind an exit handler as a catch all way of handling final tasks based on this status code.
Is it possible to dispatch on the status code from within the exit handler?
As far as I can tell, No.

Therefore, I am unable to obtain the status value, as shown in this small example:

#include <iostream>
#include <cstdlib>

int Get_Return_Code(){
  //can this be implemented?
  return 0;
}

void Exit_Handler() {

    // how do I get the return code
    // from within the exit heandler?
    auto return_code = Get_Return_Code(); //?

    // I'd like to make decisions based on the return code
    // while inside my exit handler
    if (return_code == EXIT_SUCCESS){
      std::cout << "perform exit successful tasks...\n";
    }
    else {
      std::cout << "perform exit failure tasks...\n";
    }
}

int main(int argc,  char** argv) 
{
    //bind the exit handler routine
    if (std::atexit(Exit_Handler)){
      std::cerr << "Registration failed\n";
      return EXIT_FAILURE;
    }

    //if an argument is passed, exit with success
    //if no argument is passed, exit with failure
    if (argc > 1){
      std::cout << "exiting with success\n";
      return EXIT_SUCCESS;
    }

    std::cout << "exiting with failure\n";
    return EXIT_FAILURE;
}

Is there a reason that C++ does not yet include on_exit?
I'm concerned about cross-compatibility in the windows world.

in regards to the code base:
My goal for doing this, does not involve memory management. We have an existing code base. There are exit statements everywhere. When a program exits with error, I'd like to display what that error code means to the user. In my mind, this is the fastest solution without major refactoring.

like image 339
Trevor Hickey Avatar asked Apr 15 '16 15:04

Trevor Hickey


People also ask

What happens when exit_code is EXIT_FAILURE?

If exit_code is EXIT_FAILURE, an implementation-defined status indicating unsuccessful termination is returned. In other cases implementation-defined status value is returned. Stack is not unwound: destructors of variables with automatic storage duration are not called.

What is the difference between Exit (0) and exit (1)?

Reports the termination when some error or interruption occurs during the execution of the program. The usage of exit (0) is fully portable. The usage of exit (1) is not portable. EXIT_SUCCESS is defined by the standard to be zero. EXIT_FAILURE is not restricted by the standard to be one, but many systems do implement it as one.

What are the types of exit status in C/C++?

There are two types of exit status in C/C++: 1 Exit Success: Exit Success is indicated by exit (0) statement which means successful termination of the program, i.e. 2 Exit Failure: Exit Failure is indicated by exit (1) which means the abnormal termination of the program, i.e. some error... More ...

What is EXIT FAILURE in Linux?

Exit Failure: Exit Failure is indicated by exit (1) which means the abnormal termination of the program, i.e. some error or interrupt has occurred. We can use different integer other than 1 to indicate different types of errors. Reports the successful termination/completion of the program. Reports the abnormal termination of the program.


1 Answers

Because cleanup tasks are supposed to be performed by your destructors, and your code is supposed to gracefully return from any scope under all circumstances (be it via return or throw).

at_exit is an anti-pattern in a RAII-friendly world.

If you want to perform some logic depending on what you're about to return from main, simply perform it when you're about to return from main. In main.

like image 122
Lightness Races in Orbit Avatar answered Oct 05 '22 21:10

Lightness Races in Orbit