Standard library for converting an object providing operator << to std :: string
I just noticed that I am using the following code in my C ++ 11 application (which works quietly):
template <typename T> std::string output_streamable_to_string(T const& kObject) { std::ostringstream os; os << kObject; return os.str(); }
So my question is, is there a function in the standard library ( std
) that provides this functionality?
I know it std::to_string
exists, but only works with standard library data types. I want to convert a boost::asio::ip::udp::endpoint
to std::string
. Since boost::asio::ip::udp::endpoint
only provides a function operator<<
, I need to convert std::ostream
to std::string
.
source share
No, there is no such function in standard C ++. But since you are using boost - you can use boost :: lexical_cast or just use your own.
source share