Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

20.2 Defining Traits and Facets for User-Defined Types

A user-defined type requires its own traits class, its own code conversion facet, and its own character classification facet. The traits class defines a conversion state_type and also defines operations such as copying arrays of the type and comparing arrays. The code conversion facet provides conversion to and from simple chars. The ctype facet provides character classification and manipulation routines, including the method for converting the new type to and from simple chars.

The following code shows a traits class declaration for Echar:

See the Class Reference section on char_traits for a complete description of the member functions in a traits class.

To create a new code conversion facet, you must inherit from the codecvt template and then provide implementations for all the protected virtual functions. Iostreams calls the protected functions through the public interface defined for codecvt. You must also provide a constructor taking a single size_t argument, and initialize codecvt with that argument.

A code conversion facet for Echar has a declaration that looks like this:

See Chapter 19 on defining a code conversion facet for more details. Also see the Class Reference section on codecvt for a complete description of member functions.

To create a character classification facet, you must inherit from the ctype template and provide implementations for all protected virtual functions. You must also provide a constructor taking a single size_t argument, and initialize ctype with that argument.

Note that the widen functions define conversions from simple chars to the user-defined character type, and the narrow function provides conversions from the user-defined type to simple chars.

A character classification facet for Echar has a declaration that looks like this:


Previous fileTop of DocumentContentsIndexNext file

OEM Edition, ©Copyright 1999, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.