Listing 1: Using pword to store strings
int my_index() { static int n = std::ios_base::xalloc(); return n; } void my_callback(std::ios_base::event ev, std::ios_base& b, int n) { std::string* p = (std::string*) b.pword(my_index()); if (ev == std::ios_base::erase_event) { delete p; b.pword(n) = 0; } else if (ev == std::ios_base::copyfmt_event && p != 0) b.pword(n) = new std::string(*p); } std::string get_string(std::ios_base& b) { std::string* p = (std::string*) b.pword(my_index()); return p ? *p : std::string(""); } void set_string(std::ios_base& b, std::string s) { int n = my_index(); int registered = b.iword(n); if (!registered) { b.iword(n) = 1; b.register_callback(my_callback, n); } delete (std::string*) b.pword(n); b.pword(n) = new std::string(s); }