is it possible to make std::string always hold a lower-case string?
here’s how I would use it:
typedef std::basic_string<...> lowercase_string;
void myfunc()
{
lowercase_string s = "Hello World"; // notice mixed case
printf(s.c_str()); // prints "hello world" in lowercase
std::string s2 = s;
printf(s2.c_str()); // prints "hello world" in lowercase
}
You can write your own char traits and pass it to
std::basic_stringas second template argument.Here is a minimal example:
And then define a typedef as:
And here is a test program:
Output:
Cool, isn’t it?
Note that to have a fully-working lowercase string class, you may need to define other functionality of
lowercase_char_traitsalso, depending on what behavior you want out of such class.Have a look at the Herb Sutter brilliant article for details and explanation:
Hope that helps.