When constructing a std::string from a const char*, I often use the following pattern:
const char* p = GetString();
std::string s(p);
I suppose I could use the similar pattern:
const char* p = GetString();
std::string s = p;
But, when I want to assign, rather than construct, to a std::string from a const char*, I have too many choices:
s = p;
s.assign(p);
std::string(p).swap(s);
Are the choices above more-or-less equivalent? Which should I prefer, and why?
Go for readability, and just use the idiomatic
operator=for assignment. Also, directly construct thestd::stringfrom theconst char*.