1// Copyright 2017, 2020 Peter Dimov.
2// Distributed under the Boost Software License, Version 1.0.
3// https://www.boost.org/LICENSE_1_0.txt
4
5#if defined(_MSC_VER)
6# pragma warning( disable: 4244 ) // conversion from float to int, possible loss of data
7#endif
8
9#include <boost/variant2/variant.hpp>
10#include <boost/core/lightweight_test.hpp>
11#include <boost/config.hpp>
12#include <boost/config/workaround.hpp>
13#include <utility>
14
15struct X: boost::variant2::variant<int, float>
16{
17#if BOOST_WORKAROUND( BOOST_MSVC, < 1940 )
18
19 template<class T> explicit X( T&& t ): variant( std::forward<T>( t ) ) {};
20
21#else
22
23 using variant::variant;
24
25#endif
26};
27
28template<class... T> struct Y: boost::variant2::variant<T...>
29{
30 using boost::variant2::variant<T...>::variant;
31};
32
33int main()
34{
35 {
36 X v1( 1 );
37 X const v2( 3.14f );
38
39 BOOST_TEST_EQ( (visit( []( int x1, float x2 ){ return (int)(x1 * 1000) + (int)(x2 * 100); }, v1, v2 )), 1314 );
40
41 visit( f: []( int x1, float x2 ){ BOOST_TEST_EQ( x1, 1 ); BOOST_TEST_EQ( x2, 3.14f ); }, v1, v2 );
42 visit( f: []( int x1, float x2 ){ BOOST_TEST_EQ( x1, 1 ); BOOST_TEST_EQ( x2, 3.14f ); }, v1: std::move(v1), v2: std::move(v2) );
43 }
44
45 {
46 Y<int, float> v1( 1 );
47 Y<int, float> const v2( 3.14f );
48
49 BOOST_TEST_EQ( (visit( []( int x1, float x2 ){ return (int)(x1 * 1000) + (int)(x2 * 100); }, v1, v2 )), 1314 );
50
51 visit( f: []( int x1, float x2 ){ BOOST_TEST_EQ( x1, 1 ); BOOST_TEST_EQ( x2, 3.14f ); }, v1, v2 );
52 visit( f: []( int x1, float x2 ){ BOOST_TEST_EQ( x1, 1 ); BOOST_TEST_EQ( x2, 3.14f ); }, v1: std::move(v1), v2: std::move(v2) );
53 }
54
55 return boost::report_errors();
56}
57

source code of boost/libs/variant2/test/variant_visit_derived.cpp