title: How do you run a function on exit in C++
body: <p>I have a function that I want to run whenever my program exits:</p>
  
  <pre><code>void foo() {
    std::cout&lt;&lt; "Exiting" &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>How do I register it to be run whenever the program exists, regardless of when and why it exits - due to signal, exit() call, etc?</p>