Apache C++ Standard Library User's Guide

## 37.1 Defining Callback Functions

The Standard Iostreams use callback functions to allow a program to respond to certain events. This allows programs to easily manage any dynamically allocated memory used by the stream objects. A program registers a callback by providing an ordinary function with an appropriate signature and calling `ios_base::register_callback()` with a pointer to the function and an index into the stream object's array of words. The registered callback function is then called any time one of the following occurs:

- Destruction of a stream;`std::ios_base::~ios_base()` is called.

- Imbuing a locale; `std::ios_base::imbue()` is called.

- Copying the stream state; `std::basic_ios<>::copyfmt()` is called.