Previous fileTop of DocumentContentsIndex pageNext file
Apache C++ Standard Library User's Guide

41.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 Apache C++ Standard Library Reference Guide 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 std::codecvt template and then override some or all the protected virtual functions. Iostreams calls the protected functions through the public interface defined for std::codecvt. You must also provide a constructor taking a single std::size_t argument, and initialize codecvt with that argument.

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

See Chapter 40 on defining a code conversion facet for more details. Also see the Apache C++ Standard Library Reference Guide section on codecvt for a complete description of member functions.

To create a character classification facet, you must inherit from the std::ctype template and provide implementations for all protected virtual functions. You must also provide a constructor taking a single std::size_t argument, and initialize std::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 DocumentContentsIndex pageNext file