Specializations of std::char_traits
Matt Austern
austern@apple.com
Wed Jul 16 19:11:00 GMT 2003
On Wednesday, July 16, 2003, at 11:11AM, Nathan Myers wrote:
> On Tue, Jul 15, 2003 at 04:39:04PM -0700, Matt Austern wrote:
>> We've received several complaints from customers who noticed that
>> std::char_traits<int> (or long, or signed char, or unsigned short, or
>> whatever) used to be defined but isn't defined anymore.
>>
>> Now, just to be obvious, I realize that
>> - The C++ standard doesn't say there should be any specializations
>> other than char and wchar_t
>> - In general there's no way to define all of the members of
>> std::char_traits for an arbitrary type. There's no good way to say
>> what char_traits<T>::int_type should be, for example.
>
> As I recall, the standard requires a default definition of
> char_traits<T> for any T. (I recall the vote in favor, which I
> opposed, if mildly.)
Yes and no. There must be a class template std::char_traits<T>
(there has to be a primary template to specialize!), but I don't see
anything in the standard saying that the primary template has to
have any of the members found in table 37. In fact, I'm not even
convinced there's a requirement that the primary template has to
be instantiable.
The standard is quite explicit about the member functions in the
std::char_traits<char> and std::char_traits<wchar_t> specializations
(clauses 21.1.3.1 and 21.1.3.2), but I believe users are entirely on
their own if they try to instantiate std::char_traits for any other
types.
I suggest that we should have a semi-useful primary template.
However, I also suggest that this primary template should define
only some of the members that the specializations do. I don't think
there's any useful definition of std::char_traits<T>::state_type for
arbitrary T, for example. Ditto for eof(), pos_type, and everything
that has to do with int_type.
--Matt
More information about the Libstdc++
mailing list