Apache C++ Standard Library User's Guide

## 37.2 An Example

A program can register a callback function on a particular stream as follows:

    // Callback function
    void show_event(std::ios_base::event e,
                    std::iosbase& io, int index)
    {
      if (e == std::ios_base::imbue_event)
        std::cout << "imbue called" << std::endl;
      else if (e == std::ios_base::erase_event)
        std::cout << "stream destroyed" << std::endl;
      else
        std::cout << "cpyfmt called" << std::endl;
    }

    std::ostringstream s;
    s.register_callback(show_event,0);         //1
    s.imbue(std::locale::global());            //2

`//1` | The function `show_event` is now called with either `std::ios_base::erase_event`, `std::ios_base::imbue_event`, or `std::ios_base::copyfmt_event` as the first argument, depending on whether the destruction of the stream, the imbuing of a new locale, or a call to `std::basic_ios<>::copyfmt()` initiated the callback.

`//2`

This causes `show_event` to be called. The first argument is `std::ios_base::imbue_event`; the second argument is a reference to the stream where the event occurred, which is `s` in this case; and the third argument is always the index provided in the call to `std::ios_base<>::register_callback()`, which is `0` in this case.
If more than one function is registered, functions are called in the opposite order of registration.

Please refer to Section 26.9.2 for an example using callback functions with a user-defined stream inserter.