File: overload_selection.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (215 lines) | stat: -rw-r--r-- 5,542 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//-----------------------------------------------------------------------------
// boost-libs variant/test/variant_get_test.cpp source file
// See http://www.boost.org for updates, documentation, and revision history.
//-----------------------------------------------------------------------------
//
// Copyright (c) 2016-2025 Antony Polukhin
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)


// This test suite was created to cover issues reported in:
//      https://svn.boost.org/trac/boost/ticket/5871
//      https://svn.boost.org/trac/boost/ticket/11602

#include "boost/variant/variant.hpp"
#include "boost/variant/recursive_variant.hpp"
#include "boost/core/lightweight_test.hpp"

#include <string>
#include <list>

struct A{};
struct B{};
struct C{};
struct D{};


bool foo(const boost::variant<A, B>& ) {
    return false;
}

bool foo(const boost::variant<C, D>& ) {
    return true;
}

void test_overload_selection_variant_constructor() {
    D d;
    BOOST_TEST(foo(d));

    boost::variant<B, A> v;
    BOOST_TEST(!foo(v));
}

// Pre msvc-14.0 could not dustinguish between multiple assignment operators:
//      warning C4522: 'assignment_tester' : multiple assignment operators specified
//      error C2666: variant::operator =' : 3 overloads have similar conversions
// Old versions of GCC have same issue:
//      error: variant::operator=(const T&) cannot be overloaded
#if (defined(__GNUC__) && (__GNUC__ < 4)) || (defined(_MSC_VER) && _MSC_VER < 1900)

void test_overload_selection_variant_assignment() {
    BOOST_TEST(true);
}

#else

struct assignment_tester: boost::variant<C, D>, boost::variant<B, A> {
    using boost::variant<B, A>::operator=;
    using boost::variant<C, D>::operator=;
};

void test_overload_selection_variant_assignment() {
    A a;
    assignment_tester tester;
    tester = a;
    const int which0 = static_cast< boost::variant<B, A>& >(tester).which();
    BOOST_TEST(which0 == 1);

    boost::variant<A, B> b;
    b = B();
    tester = b;
    const int which1 = static_cast< boost::variant<B, A>& >(tester).which();
    BOOST_TEST(which1 == 0);
}

#endif

typedef boost::variant<int> my_variant;

struct convertible {
    operator my_variant() const {
        return my_variant();
    }
};

void test_implicit_conversion_operator() {
    // https://svn.boost.org/trac/boost/ticket/8555
    my_variant y = convertible();
    BOOST_TEST(y.which() == 0);
}

struct X: boost::variant< int > {};
class V1: public boost::variant<float,double> {};

struct AB: boost::variant<A, B> {};

void test_derived_from_variant_construction() {
    // https://svn.boost.org/trac/boost/ticket/7120
    X x;
    boost::variant<X> y(x);
    BOOST_TEST(y.which() == 0);

    // https://svn.boost.org/trac/boost/ticket/10278
    boost::variant<V1, std::string> v2 = V1();
    BOOST_TEST(v2.which() == 0);

    // https://svn.boost.org/trac/boost/ticket/12155
    AB ab;
    boost::variant<AB, C> ab_c(ab);
    BOOST_TEST(ab_c.which() == 0);

    boost::variant<A, B> a_b(ab);
    BOOST_TEST(a_b.which() == 0);

    boost::variant<B, C, A> b_c_a1(static_cast<boost::variant<A, B>& >(ab));
    BOOST_TEST(b_c_a1.which() == 2);


//  Following conversion seems harmful as it may lead to slicing:
//  boost::variant<B, C, A> b_c_a(ab);
//  BOOST_TEST(b_c_a.which() == 2);
}

void test_derived_from_variant_assignment() {
    // https://svn.boost.org/trac/boost/ticket/7120
    X x;
    boost::variant<X> y;
    y = x;
    BOOST_TEST(y.which() == 0);

    // https://svn.boost.org/trac/boost/ticket/10278
    boost::variant<V1, std::string> v2;
    v2 = V1();
    BOOST_TEST(v2.which() == 0);

    // https://svn.boost.org/trac/boost/ticket/12155
    AB ab;
    boost::variant<AB, C> ab_c;
    ab_c = ab;
    BOOST_TEST(ab_c.which() == 0);

    boost::variant<A, B> a_b;
    a_b = ab;
    BOOST_TEST(a_b.which() == 0);

    boost::variant<B, C, A> b_c_a1;
    b_c_a1 = static_cast<boost::variant<A, B>& >(ab);
    BOOST_TEST(b_c_a1.which() == 2);


//  Following conversion seems harmful as it may lead to slicing:
//  boost::variant<B, C, A> b_c_a;
//  b_c_a = ab;
//  BOOST_TEST(b_c_a.which() == 2);
}


// http://thread.gmane.org/gmane.comp.lib.boost.devel/267757
struct info {
    struct nil_ {};

    typedef
        boost::variant<
            nil_
          , std::string
          , boost::recursive_wrapper<info>
          , boost::recursive_wrapper<std::pair<info, info> >
          , boost::recursive_wrapper<std::list<info> >
        >
    value_type;
    value_type v;

    inline void test_on_incomplete_types() {
        info i0;
        i0.v = "Hello";
        BOOST_TEST(i0.v.which() == 1);

        info::value_type v0 = "Hello";
        BOOST_TEST(v0.which() == 1);

        info::value_type v1("Hello");
        BOOST_TEST(v1.which() == 1);

        info::value_type v2 = i0;
        BOOST_TEST(v2.which() == 2);

        info::value_type v3(i0);
        BOOST_TEST(v3.which() == 2);

        v0 = v3;
        BOOST_TEST(v0.which() == 2);

        v3 = v1;
        BOOST_TEST(v3.which() == 1);

        v3 = nil_();
        BOOST_TEST(v3.which() == 0);
    }
};



int main()
{
    test_overload_selection_variant_constructor();
    test_overload_selection_variant_assignment();
    test_implicit_conversion_operator();
    test_derived_from_variant_construction();
    test_derived_from_variant_assignment();
    info().test_on_incomplete_types();

    return boost::report_errors();
}