Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

std::char_traits and custom character set

20 views
Skip to first unread message

bitrex

unread,
Oct 6, 2017, 9:28:50 AM10/6/17
to
Is there a way to extend std::char_traits to define my own custom
character sets, using some fundamental type as storage, such that I can
then use the usual iostream and string operations on a std::basic_string
which is templated with the custom type?

Just for the sake of argument say I wanted some kind of set where all
the printable letters of the alphabet were mapped to the integers modulo
ten, so if I instantiated a std::basic_string with i.e. "abcde" that
would be converted to "01234", and the latter is what I would get if I
used cout or the methods std::basic_string::data(),
std::basic_string::c_str(), etc.

bitrex

unread,
Oct 6, 2017, 9:29:52 AM10/6/17
to
On 10/06/2017 09:28 AM, bitrex wrote:
> Is there a way to extend std::char_traits to define my own custom
> character sets, using some fundamental type as storage, such that I can
> then use the usual iostream and string operations on a std::basic_string
> which is templated with the custom type?
>
> Just for the sake of argument say I wanted some kind of set where all
> the printable letters of the alphabet were mapped to the integers modulo
> ten, so if I instantiated a std::basic_string with i.e.

^^ E.G.

James R. Kuyper

unread,
Oct 6, 2017, 11:02:53 AM10/6/17
to
On 2017-10-06 09:28, bitrex wrote:
> Is there a way to extend std::char_traits to define my own custom
> character sets, using some fundamental type as storage, such that I can
> then use the usual iostream and string operations on a std::basic_string
> which is templated with the custom type?

Yes. char_traits was specifically designed with such usage in mind.
Define a POD class type of your own that uses that fundamental type as
its storage unit, and then specialize std::char_traits<> for that class
type, meeting all of the requirements specified in table 62 of the
standard. If you do that, you should be able to use std::basic_string
with your character type.

> Just for the sake of argument say I wanted some kind of set where all
> the printable letters of the alphabet were mapped to the integers modulo
> ten, so if I instantiated a std::basic_string with i.e. "abcde" that
> would be converted to "01234", and the latter is what I would get if I
> used cout or the methods std::basic_string::data(),
> std::basic_string::c_str(), etc.

That, on the other hand, I doubt you can do. I'd have to trace through a
large portion of the standard to be sure, but I believe that the string
oriented stuff all is designed on the assumption that characters are
constant: if you store an 'a' in a string, you must get back an 'a' when
you retrieve it from the string. You shouldn't get back a '0'. You
shouldn't try to figure out a way to make std::basic_string do this
implicitly - you should create code that explicitly performs the
conversion your talking about.

bitrex

unread,
Oct 6, 2017, 3:51:25 PM10/6/17
to
Thanks for clearing up the stuff in your last paragraph, that makes
sense. So say my data type could use char as the fundamental type but
had some peculiarities like certain characters should be considered
materially equivalent to each other, like capital letters and lowercase
for example.

Then as I understand it I could public inherit std::char_traits<char>
for my own type and just override the methods that do comparison leaving
the functions for copying, etc. as is as they would be for a standard
ASCII char set.

If I then need some custom mapping between what the .data() method of
std::basic_string returns and what I need I guess I could just add
another static method to my char_traits implementation and call that
explicitly? It looks like every method in the class is static and it
isn't actually instantiated anywhere.

The mapping function could certainly be a free function or a member of
something else but it seems logical from an organizational perspective
to put it with the rest of the custom implementation.
0 new messages