3

Searched around for this, but the best result has only been to copy the string.

Given this simple snippet:

struct custom_string_traits: std::char_traits<char> {
    static bool eq(char c, char d) { ... }
    static bool lt(char c, char d) { ... }
    ...
};
typedef std::basic_string<char,custom_string_traits> my_string;

how might I perform operations such as

std::string foo = "baz";
my_string bar = "qux";
x::swap(foo, bar);
/*  OR  */
foo = std::move(bar);

I want to optimize away from my current procedure:

std::string foo = "baz";
my_string bar = "qux";
my_string next(foo.c_str(), foo.size());
foo.assign(bar.c_str(), bar.size());

Looked at C++ std::basic_string/char_traits specialization; not sure how to interpret it.

TekuConcept
  • 1,264
  • 1
  • 11
  • 21
  • Copying is does indeed appear to be the only way. Personally, I would use the iterator constructor, but I suppose that's just a matter of taste. You can avoid copying by using the same char_traits throughout. – eerorika Jan 05 '19 at 02:29
  • @eerorika I have a case sensitive string (std::string) I need to convert to a case insensitive string (my_string). Otherwise I would use the same char_traits. – TekuConcept Jan 05 '19 at 02:36
  • 1
    And you need to copy to do that. You've shown the optimal solution. – eerorika Jan 05 '19 at 02:37

0 Answers0