forked from boostorg/variant2
Add operator<< for variant
This commit is contained in:
@ -25,6 +25,7 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
#include <functional> // std::hash
|
#include <functional> // std::hash
|
||||||
#include <cstdint>
|
#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)... ) } );
|
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
|
// hashing support
|
||||||
|
|
||||||
namespace detail
|
namespace detail
|
||||||
|
@ -123,3 +123,5 @@ run variant_visit_r.cpp : : :
|
|||||||
compile variant_derived_construct.cpp ;
|
compile variant_derived_construct.cpp ;
|
||||||
|
|
||||||
run variant_visit_by_index.cpp ;
|
run variant_visit_by_index.cpp ;
|
||||||
|
|
||||||
|
run variant_ostream_insert.cpp ;
|
||||||
|
36
test/variant_ostream_insert.cpp
Normal file
36
test/variant_ostream_insert.cpp
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
// Copyright 2021 Peter Dimov
|
||||||
|
// Distributed under the Boost Software License, Version 1.0.
|
||||||
|
// https://www.boost.org/LICENSE_1_0.txt
|
||||||
|
|
||||||
|
#include <boost/variant2/variant.hpp>
|
||||||
|
#include <boost/core/lightweight_test.hpp>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
using namespace boost::variant2;
|
||||||
|
|
||||||
|
template<class T> std::string to_string( T const& t )
|
||||||
|
{
|
||||||
|
std::ostringstream os;
|
||||||
|
|
||||||
|
os << t;
|
||||||
|
|
||||||
|
return os.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
variant<int, float, std::string> v( 1 );
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( to_string( v ), to_string( 1 ) );
|
||||||
|
|
||||||
|
v = 3.14f;
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( to_string( v ), to_string( 3.14f ) );
|
||||||
|
|
||||||
|
v = "test";
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( to_string( v ), to_string( "test" ) );
|
||||||
|
|
||||||
|
return boost::report_errors();
|
||||||
|
}
|
Reference in New Issue
Block a user