#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();
}
如何移动 std::ostringstream 的基础字符串对象?
s = move( oss.rdbuf()->str() )
来实现[哦,我检查了,不行],但是这样做会冒着从流代码中拉出地毯的风险,从而破坏其假设。你为什么想要这个? - Cheers and hth. - Alfnode handle
创造出一些有趣的东西:http://en.cppreference.com/w/cpp/container/node_handle - alexeykuzmin0std::basic_stringbuf
使用std::basic_string
作为其底层缓冲区(即使实际上它是这样做的)。 - user657267str
成员函数的全部内容。但它不提供对其进行可变访问的方式。嗯。 - Cheers and hth. - Alfbasic_string
的实现以及一个使用普通的char
数组的实现。 - T.C.