# Converting between regular STL string and re-templated version

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

## Recommended Posts

I have a case-insensitive string, which is just a re-templated version of std::basic_string as described here. It works, and I understand how it works. However, I would like to be able to do something like this:
std::string normalString("Hello, World!");
ci_string caseInsensitive = normalString;

This generates compiler errors. Any ideas on how (or where to define an operator = to make this work? Thanks very much.

##### Share on other sites
Providing the errors will help people here to diagnose your problem.

In any case, std::string and ci_string are two different types; they start from the same basic_string template, but the template parameters are different, so these two types are considered different by the compiler. (You may know yourself that they are internally identical, but the compiler can't easily know that.)

You need a conversion method. One way to do this without changing existing definitions is like this:

ci_string MakeCIString(const std::string& s){   return ci_string(s.c_str());}std::string normalString("Hello, world!");ci_string caseInsensitive = MakeCIString(normalString);

The compiler should be able to optimize this by inlining and potentially the return-value optimization (though I don't remember the specific circumstances to which that applies).

Another way would be to change how you define ci_string, by inheritance rather than a typedef, and add a conversion constructor:

class ci_string : public std::basic_string<char,ignorecase_traits>{public:   ci_string(const std::string& s) { /* behaviour similar to copy constructor */ }};std::string normalString("Hello, world!");ci_string caseInsensitive = normalString;

##### Share on other sites
I would probably inherit from ci_string and add a constructor, an operator=(std::string), as well as an operator to cast to std::string.

##### Share on other sites
OK, thanks. I guess I was just hoping that there was some easy way to overload the = operator, but oh well [smile]. Not that big of a deal, anyways.

##### Share on other sites
Can you initialize ci_string with C strings?

ci_string caseInsensitive = normalString.c_str();

##### Share on other sites
Quote:
 Original post by dbzprogrammerCan you initialize ci_string with C strings?ci_string caseInsensitive = normalString.c_str();
If it's derived from std::string, that'll work fine. However, it's not quite as intuative as being able to use it without the .c_str(), and it'll be slightly slower - particularly if there's a fast custom allocator in use, since allocating from a const char* requires a call to strlen() before allocating memory, which could be slow for large strings.

EDIT: Ah, I see you're just changing the traits...

##### Share on other sites
There's no way to achieve the syntax you want without subclassing.

However, you can do this:

//construction
ci_string caseInsensitive(normalString.begin(),normalString.end());

//assignment
caseInsensitive.assign(normalString.begin(),normalString.end());

##### Share on other sites
Herb Sutter discusses case-insensitive strings in Exceptional C++. One of the points he makes is that although there are places where it makes sense to have case-insensitivity as a property of a string object more often case insensitivity is better expressed as part of the comparison only, i.e. using a stricmp function instead of operator==.

When you make case-insensitivity a part of the object there are additional problems that must be considered. Should a case insensitive string be case-preserving? If so then strings which compare equal can display differently. If not then the string is more than just case-insensitive, it's case-modifying. Should a comparison between a case-insensitive and a case-sensitive string be case-sensitive or not? How about a comparison between a case-insensitive string and some other type of string with yet another different ordering?

If your case-insensitive string is for a real project rather than just investigation then you should consider what implications having case-insensitivity as part of the string object has for your specific situation.

Σnigma

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 12
• 12
• 20
• 12
• 13
• ### Forum Statistics

• Total Topics
632150
• Total Posts
3004454

×