The std::string
is extremely useful. However, as soon as people need a string class with slightly different semantics for string handling, some tend to write their own string class.
Writing your own string class is rarely a good idea because safe string handling is hard. Fortunately, std::string
is only a specializing typedef of the template class, std::basic_string
. This class contains all the complicated memory handling stuff, but it does not impose any policy on how strings are copied, compared, and so on. This is something that is imported into basic_string
by accepting a template parameter that contains a traits class.
In this recipe, we will see how to build our own trait classes and, this way, how to create custom strings without reimplementing anything.