Topic: char_traits<> and locales


Author: "Martin Connell" <topology@btinternet.com>
Date: 1997/11/09
Raw View
After some indepth work with the Standard C++ Libary char_traits<>,
locale<>, and basic_string<> classes, I have come to the conclusion that the
char_traits<> concept, as it stands, is more or less useless.  My reasoning
for this thinking follows:-

When starting out with the basic_string<> class (PJ Plauger's in VC++5.0), I
was impressed by the char_traits<> concept and thought it would offer help
in terms of doing localised string collations and the like.

Now I am at the point of actually trying to implement such a thing.  My goal
is a string class that can be imbued with a locale object and is thereby
capable of automatic locale-sensitive collation, ctype<> processing,
wide-to-narrow-to-wide conversion, etc.  As I said, at first it seemed
straight forward enough: derive a class from a basic_string<> which is
specialised using a custom char_traits<> class, this last custom class
handling the locale-sensitive stuff.  Great in theory, but this doesn't seem
to be possible in practice.  char_traits<> is intended to be used as a
purely static class and is therefore unable to store any state information
(such as a locale object).

So what is the point of char_traits<>.  Given that all its members are
static, it seems to me there is no scope whatsoever for extending this class
beyond the standard char and wchar_t specialisations supplied with the
library.  The nub of it is that the char_traits<char>::widen() methods and
the char_traits<wchar_t>::narrow() methods can, at best, use the system
default locale information so anyone who wants locale-dependent strings and
streams using this method is whistling in the wind.

Or have I overlooked something?

Regards,
Martin Connell
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]