I have a function that is coded to output text to standard output when the destructor of the global basic_ostream object is called. I've registered this function on the stream but for some reason it doesn't do anything:
void callback(std::ios_base::event evt, std::ios_base& str, int index)
{
if (evt == std::ios_base::erase_event)
{
std::cout << "Erase event";
}
}
int main()
{
std::cout.register_callback(callback, index());
}
There are no errors/warnings and it doesn't output anything unless I add an explicit call to the destructor:
std::cout.~basic_ostream<char>(); // "Erase event"
I realize that doing so is wrong so I won't count that as a workaround. I've even tried flushing the output from the buffer but to no avail. Why is this happening? I'm running my code on GCC 4.8. Here is a demo.