One of the fundamental differences between the new, templated iostreams and the traditional iostreams is the ability of the new iostreams to accommodate user-defined character types. It's now possible to use the C++ iostreams interface with arbitrary character-like types, as well as with ordinary chars.
Of course, this flexibility comes at a price. Any user-defined character type must satisfy rather severe requirements. The type really must act like an ordinary char in most circumstances. In addition, several classes must be defined to support the type in the iostreams environment.
In general, user-defined character types must meet the following requirements:
They must have a public default constructor.
They must have a public copy constructor.
They must have a public destructor.
They must be convertible to int.
They must be assignable.
They must be comparable for equality and ordering.
In addition, it must be possible to convert an object of the user-defined type to an object of type char and vice versa. These particular conversions need not be part of the type itself. They are defined as part of the new character classification facet that must accompany the new type. We'll examine these conversions a little later on.
The following type satisfies the requirements for a user-defined type:
struct Echar { Echar():c(0),i(0) {;} Echar(char cc):c(cc),i(0) {;} Echar(char cc,char ii):c(cc),i(ii) {;} operator int() { return c; } char c; char i; }; bool operator==(const Echar& lhs, const Echar& rhs) { return lhs.c == rhs.c; } bool operator!=(const Echar& lhs, const Echar& rhs) { return !(lhs.c == rhs.c); } bool operator<(const Echar& lhs, const Echar& rhs) { return lhs.c < rhs.c; }
OEM Edition, ©Copyright 1999, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.