Listing 4
#include "streamstate.h"
#include <iostream>
#include <ostream>
using namespace std;
// the "state" object as in Listing 3.
struct bracketing { /* ... */ };
// this encapsulates the access to additional state
streamstate_value<bracketing> bracketing_state_;
// the hypothetical complex class
class mycomplex
{
public:
ostream& print(ostream &os) const
{
// get the stream state from the slot dedicated to "bracketing"
bracketing &b = bracketing_state_.get(os);
return os << b.left_ << re_ << ", " << im_ << b.right_;
}
// other operations and members as in Listing 1.
};
// inserter for mycomplex and setbrackets as in Listing 1.
// ...
ostream & operator<<(ostream &os, bracketing b)
{
// set the stream state in the private slot
bracketing_state_.get(os) = b;
return os;
}
int main() { /* ... */ }