Is it legal to modify the result of std::string::op[]?
I would like to swap the contents of char* and std::string and could not do it. After going through the above link, I'm still unable to get the answer for my question.
This is what I am trying to do and could not succeed in swapping the string with char*. The code is verified on VS2010.
UPDATE 1 All the effort is to see if I could gain any performace and not gain any SO points. :-)
UPDATE 2 I mentioned in the comments why I intend to do this. Do people still believe that this requires to be on hold ?
void Swap(char* const& left, char* const& right)
{
std::swap(const_cast<char*>(left), const_cast<char*>(right));
}
std::string ToString()
{
std::string ret(1, ' ');
char* str = new char[6]();
strcpy(str, "Jagan");
Swap(&ret[0], str);
return ret;
}