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.