we have a multi-threaded desktop application in C++ (MFC). Currently developers use either CString or std::string, probably depending on their mood. So we\'d like to choose a s
std::string
is usually reference counted, so pass-by-value is still a cheap operation (and even more so with the rvalue reference stuff in C++0x). The COW is triggered only for strings that have multiple references pointing to them, i.e.:
std::string foo("foo");
std::string bar(foo);
foo[0] = 'm';
will go through the COW path. As the COW happens inside operator[]
, you can force a string to use a private buffer by using its (non-const) operator[]()
or begin()
methods.