While implementing C++1z's std::basic_string_view to use it on older compilers, I encountered a problem with the stream output operator overload for it.
Basically, it has to output the contents referenced by the string_view while not relying on any null-terminator being present (as string_view is not guarantueed to be null-terminated).
Normally, writing overloads for operator<< is quite easy since you can rely on already present overloads, and thus do not need to use sentry objects as mentioned in this question on SO.
But in this case, there is no predefined overload for operator<< taking a character pointer and a length (obviously). Because of this I create a temporary std::string instance in my current implementation:
template< typename TChar, typename TTraits >
auto operator<<(::std::basic_ostream<TChar, TTraits>& p_os, basic_string_view<TChar, TTraits> p_v)
-> ::std::basic_ostream<TChar, TTraits>&
{
p_os << p_v.to_string(); // to_string() returns a ::std::string.
return p_os;
}
This works, but I really dislike the fact that I have to create a temporary std::string instance, because that entails redundantly copying the data and potential usage of dynamic memory. This, in my opinion at least, defeats the purpose of using a lightweight reference type.
So my question is:
What is the best way to implement correct formatted output for my string_view without the overhead?
While researching, I found that LLVM does it like this: (found here)
// [string.view.io]
template<class _CharT, class _Traits>
basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>& __os, basic_string_view<_CharT, _Traits> __sv)
{
return _VSTD::__put_character_sequence(__os, __sv.data(), __sv.size());
}
The implementation of __put_character_sequence resides in this file, but it makes heavy use of internal functions to do the formatting. Do I need to reimplement all formatting by myself?
string_viewis a runtime value, I do not see how to use a custom traits type for that, since the construction of it (that I could use to supply my runtime length value) happens somewhere in theoperator<<overload you mentioned, out of my control.ios_base::width(). You'll also need to look at theleftvs.right, and (if memory serves)fixedto figure out where to insert padding, and whether to truncate to the field width (retrieve withios_base::fmtflags()). Um...oh, you'll also need to read the current fill character (withios_base::fill()) if you're going to do padding.