Jump to content
  • Advertisement
Sign in to follow this  
Nitage

C++: Why are 'char_types' requires to be POD?

This topic is 4233 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been trying to use std::basic_string and I've run into a few issues. The whole char_traits class and concept of a char_type seem to be at odds with each other: Why does basic_string require a 'char-type' (POD) type as it's element type? It doesn't seem to make much sense. You can't even use it as an ascii string class as any data cannot be private - and so the character type can't be restricted to the ascii range. Why does the char_traits class contain so many static functions? Seeing as 'char-type' objects are required to be POD types, almost all the functions in the class are redundant. Does anyone have any insight into why these things are the way they are?

Share this post


Link to post
Share on other sites
Advertisement
The fact that char_type has to be a POD actually has more to do with its use in locales, iostreamd, and strings than in the standard char_traits class. Consider the meaning of using the codecvt facet on a non-pod type, or how you could use a standard streambuf object on a non-pod type without specializing everything completely.

As to the functions on char_traits, well, that how you would implement a restricted char type. The restriction is a trait, not the type of storage in which the data fits. Both ASCII and UTF-8 will fit in a char, but the required traits would be different.

Picture how a char_traits::eq() specialization would differ between an ASCII codeset and an EBCDIC codeset.

Picture how a char_traits::find() specialization would differ between a UTF-8 codeset and a shift-JIS codeset.

Of course, encoding is best handled through locales, not the data storage used, anyways.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!