mirror of
https://github.com/boostorg/variant2.git
synced 2026-01-25 00:22:25 +01:00
Add operator<< for variant
This commit is contained in:
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