Add operator<< for variant

This commit is contained in:
Peter Dimov
2021-09-15 02:51:11 +03:00
parent aebcb9792d
commit e668c099ce
3 changed files with 63 additions and 0 deletions

View File

@ -25,6 +25,7 @@
#include <utility>
#include <functional> // std::hash
#include <cstdint>
#include <iosfwd>
//
@ -2236,6 +2237,30 @@ template<class R = detail::deduced, class V, class... F> constexpr auto visit_by
detail::visit_by_index_L<R, V, F...>{ std::forward<V>(v), std::tuple<F&&...>( std::forward<F>(f)... ) } );
}
// output streaming
namespace detail
{
template<class Ch, class Tr, class... T> struct ostream_insert_L
{
std::basic_ostream<Ch, Tr>& os;
variant<T...> const& v;
template<class I> std::basic_ostream<Ch, Tr>& operator()( I ) const
{
return os << unsafe_get<I::value>( v );
}
};
} // namespace detail
template<class Ch, class Tr, class... T> std::basic_ostream<Ch, Tr>& operator<<( std::basic_ostream<Ch, Tr>& os, variant<T...> const& v )
{
return mp11::mp_with_index<sizeof...(T)>( v.index(),
detail::ostream_insert_L<Ch, Tr, T...>{ os, v } );
}
// hashing support
namespace detail