Apache C++ Standard Library User's Guide

## 41.3 Creating and Using Streams Instantiated on User-Defined Types

Once all the support work is finished, you can instantiate a stream class template on your type and begin to use it--well, not quite. Because the new stream requires the new facets you've defined, you must first create a new locale containing these facets and then imbue that locale on your stream. You can do this as follows:

    typedef std::basic_fstream<Echar,Etraits> Estream;           // 1
    std::locale Eloc(std::locale(std::locale(),
                                 new Ecodecvt),new Ectype);      // 2
    Estream foo("foo.txt");                                      // 3
    foo.imbue(Eloc);                                             // 4

`//1` | Typedef the special stream type to something convenient.
`//2` | Construct a new locale object and replace the `std::codecvt` and `std::ctype` facets with the ones we've defined for `Echar`. We use the constructor that takes an existing locale and a new facet to construct a locale with a new `codecvt` facet, and then use it again to get a `locale` with both new facets.
`//3` | Construct an `Estream`.
`//4` | Imbue the new `locale` object containing our replacement facets onto the `Estream`.

Now we're ready to insert `Echar`s into the stream and read them back in:

    Echar e[10];
    Estream::pos_type pos = foo.tellp();                           //1
    foo << 10;                                                     //2
    foo.seekg(pos);                                                //3
    foo >> e;                                                      //4

`//1` | Get current position.
`//2` | Write out the integer `10`.
`//3` | Seek back.
`//4` | Read in the string `10` as `Echar`s.