With rvalue references appearing at the top of Return Value Optimization, what would be the most efficient way to implement such a core function? How can I improve this implementation or leave it alone?
template <typename T>
string
to_string(const T& t)
{
stringstream ss;
ss << t;
return ss.str();
}
Obviously, I want to avoid copying or allocating memory if I can. TIA.
Edit: from thanks to D. Rodriguez for this detailed answer. Now I have the second part of my question. Is there any way to improve this?
#define to_cstr( T ) (to_string( T ).c_str())
Of course, I would like to avoid MACROs if I can, but if I copy and paste the template code above to return ss.str (). c_str () and const char *, temporary does not live long enough; although the code seems to work, valgrind complains (red light).
, MACRO to_cstr(). , , ?