...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
boost::cnv::stream
accepts custom string and string-like
types as input or output as long as they satisfy certain requirements.
boost::cnv::cstream cnv; my_string my_str("123"); cnv(std::setprecision(2))(std::fixed); BOOST_TEST(convert<int>(my_str, cnv).value_or(0) == 123); BOOST_TEST(convert<my_string>( 99.999, cnv).value_or("bad") == "100.00"); BOOST_TEST(convert<my_string>( 99.949, cnv).value_or("bad") == "99.95"); BOOST_TEST(convert<my_string>(-99.949, cnv).value_or("bad") == "-99.95");
When a string-like type is the source, then it needs to be a contiguous
sequence of the corresponding character type (char
or wchar_t
)
accessible via begin()
and end()
.
std::stringstream
is implemented entirely
in terms of std::basic_streambuf
which, in turn, operates
on a contiguous character sequence, also called
the buffer (see std::basic_streambuf
for details).
For efficiency reasons boost::cnv::stream
uses the provided (read-only) input string as the buffer
and, consequently, requires that provided input string to be a contiguous
character sequence.
When a string is the target, then the described contiguous character sequence requirement does not apply. Any type that provides
MyType::MyType(char const* beg, char const* end)
constructor can be deployed in type-to-string conversions.
See The Bigger Picture chapter for the discussion of potential advantages of deploying custom strings.