I'm currently using the very clever package boost::const_string
until http://libcxx.llvm.org/ is available pre-packaged on Ubuntu or GCC make its __versa_string
(in header ext/vstring.h
) its default string implementation. libcxx's std::string
aswell as __versa_string
uses _small-string optimization (SSO) by default. Default support for outputting to an std::ostream
is lacking however. The code
#include <iostream>
#include <boost/const_string.hpp>
const_string<char> x;
std::cout << x << endl;
does not work unless we force x
into a c-string via c_str()
which becomes
std::cout << x.c_str() << endl;
which compiles and works as expected. I added the following line to const_string.hpp
template <typename T>
inline std::ostream & operator << (std::ostream & os, const boost::const_string<T> & a)
{
return os.write(a.data(), a.size());
}
This should improve performance over x.c_str()
because size()
is already known and does not need to be calculated by searching for NULL
as in c_str()
. I works for me but I am uncertain whether it works all cases. Have I missed something?
\0
), I guess. I think (!) the default behaviour for normal strings is to truncate after null chars. Your implementation probably won’t do that. By the way, +1 for making me aware ofboost::const_string
. – Refitstd::setw
. If you don't use those manipulators forconst_string
, I think your code has its own use. – Jacintha