c++castingtypedefcase-sensitivechar-traits

Convert std::string to ci_string


I used this approach to create a case-insensitive typedef for string. Now, I'm trying to convert a std::string to ci_string. All of the following throw compiler errors:

std::string s {"a"};
ci_string cis {s};
ci_string cis (s);
ci_string cis {(ci_string)s};
ci_string cis ((ci_string)s);
ci_string cis = s;

I spent some time trying to figure out how to overload the = operator, and I attempted to use static_cast and dynamic_cast without success. How can I do this?


Solution

  • Your two types are different, so you cannot use the constructor with a regular std::string. But your string is still able to copy a C string, so this should work:

    std::string s{"a"};
    ci_string cis{ s.data() }; // or s.c_str(), they are the same