#include <sstream>
#include <string>
using namespace std;
template<typename T>
string ToString(const T& obj)
{
ostringstream oss;
oss << obj;
//
// oss will never be used again, so I should
// MOVE its underlying string.
//
// However, below will COPY, rather than MOVE,
// oss' underlying string object!
//
return oss.str();
}
How to move std::ostringstream's underlying string object?
s = move( oss.rdbuf()->str() )
[ Oh, I checked, you can't], but you risk breaking the assumptions of the stream code by pulling the rug from under its feet.*. Why do you want this? – Gennode handle
from C++17: en.cppreference.com/w/cpp/container/node_handle – Korosealstd::basic_stringbuf
uses astd::basic_string
as its underlying buffer (even though this is how it's done in practice). – Ninetiethstr
member functions are all about. But it doesn't provide mutable access to it. Hm. – Genstd::basic_string
member, the standard only refers to an "underlying character sequence". – Ninetiethstringstream
to store the data in contiguious buffer AFAIK. So, to get the string, you will have to construct a string object atleast once. – Redcapbasic_string
and one that uses a plainchar
array. – Mymya