Seriously, I think your design needs rethinking if you want extra functions in a specialization. |
I guess I'll just spit it out then. Maybe you guys can give a suggestion on a design improvement.
This is for a series of string classes in a general library I'm working on. The idea is I'll have at least 3 different classes: utf8str, utf16str, and utf32str. They all function the same way, but store the strings different internally.
Additionally they're interchangable, so you can do UTF-8 to UTF-16 or whatever conversions with an assignment:
1 2 3 4 5 6
|
utf8str foo = "Boo";
utf16str bar = foo; // converts to UTF-16
// you can then get pointers for whatever:
const utf8* ptr1 = foo.c_str();
const utf16* ptr = bar.c_str();
| |
Now 99% of the interface is exactly the same for all 3 classes. And I can just have the template call non-templated overloaded functions in another class to do the conversion and type specific stuff.
The problem is i don't want a c_str function as above for type safety reasons. Specifically because I want a more generic 'utfstr' class... the underlying format of which can be changed with a compiler option. The idea is you write your code using utfstr without caring about which underlying format is used, and only use the specific utf8str etc types when you need the raw string data for specific input/output.
If I have this generic utfstr class and the above c_str function, you can easily shoot yourself in the foot by doing this:
1 2 3
|
utfstr foo = "blech";
const utf8* foo.c_str(); // shot yourself in the foot. This assumes utfstr uses UTF-8
| |
That code would compile okay
IF utfstr is configured to use UTF-8. But if you change this at a future time to UTF-16 or something, you just broke your code.
So the idea is, utf8str, utf16str, utf32str would all have a c_str (or similar but differently named) function, whereas utfstr wouldn't. Apart from that, they'd be identical.
Additionally... since utf32str is the only one that's really random access friendly, it's the only one I'd want to have a [] operator.
So to sum up:
1 2 3 4
|
utfstr : nothing additional
utf8str : c_str
utf16str: c_str
utf32str: c_str, operator []
| |
Plus I'd want the utf32str iterators to be different from the other class' iterators for the [] operator.
Any input / ideas?
I really appreciate all the help so far, guys! Thanks!