A few points:
First, instead of #ifdef CXX11_AVAILABLE
you want to check if __cplusplus > 201103L
; for an explanation, see:
http://stackoverflow.com/questions/11053960/ddg#11054055
Now, you're going to have at least one copy anyway, just by piping the string into the ostringstream
, regardless of which C++ version you're using.
Also, you're going to be creating and destroying an ostringstream
with every damn call to toString()
- that's terrible! ... at least do:
namespace detail {
inline std::ostringstream& get_ostringstream(){
static thread_local std::ostringstream stream;
stream.str("");
stream.clear();
return stream;
}
} // namespace detail
and then in toString()
, replace
std::ostringstream oss;
with
std::ostringstream& oss = detail::get_ostringstream();
Finally, remember that the final copy - the oss.str()
is subject to Return Value Optimization (RVO), which in C++17 is mandatory and for earlier standard was applied by most compilers. So if you initialize a string with the result of the function, the construction of the oss.str()
copy would take place at the address of that outside string.
Edit: Actually, if this is performance-critical code, you should just not be using general-purpose string conversion functions, and possibly not use std::string
at all. Of course - before customizing anything, profile and check where you're hurting performance-wise.