我刚刚注意到,我用我的C++应用程序11下面的代码(其中工程安静精):标准库函数的对象为操作者提供<<转换为std :: string
template <typename T>
std::string output_streamable_to_string(T const& kObject) {
std::ostringstream os;
os << kObject;
return os.str();
}
所以我的问题是:在提供这种功能的标准库(std
)中是否存在函数?
我知道std::to_string
存在,但只适用于标准库数据类型。我想将boost::asio::ip::udp::endpoint
转换为std::string
。由于boost::asio::ip::udp::endpoint
只提供了operator<<
函数,所以我确实需要将std::ostream
转换为std::string
。
恐怕它已经是标准库的方式来做到这一点:-) –
你想要的是称为序列化。看看[protobuf](https://developers.google.com/protocol-buffers/docs/overview),[msgpack](https://github.com/msgpack/msgpack-c)或类似[picojson serializer] (https://github.com/d-led/picojson_serializer) –
@Dmitry Ledentsov:使用序列化库/框架完全是**过度杀毒。我只想将一个简单的数据类型或对象转换为一个'std :: string'。 @ForEveR回答了这个问题。我知道你提到的前两个库。 –