aboutsummaryrefslogtreecommitdiff
path: root/test/std/utilities/tuple/tuple.tuple/tuple.cnstr/PR22806_constrain_tuple_like_ctor.pass.cpp
blob: 0d3b7ff24edb12e1be2be618c327540472ad597a (plain) (blame)
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
//===----------------------------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: c++98, c++03

// <tuple>

// template <class... Types> class tuple;

// template <class TupleLike>
//   tuple(TupleLike&&);
// template <class Alloc, class TupleLike>
//   tuple(std::allocator_arg_t, Alloc const&, TupleLike&&);

// Check that the tuple-like ctors are properly disabled when the UTypes...
// constructor should be selected. See PR22806.

#include <tuple>
#include <memory>
#include <cassert>

template <class Tp>
using uncvref_t = typename std::remove_cv<typename std::remove_reference<Tp>::type>::type;

template <class Tuple, class = uncvref_t<Tuple>>
struct IsTuple : std::false_type {};

template <class Tuple, class ...Args>
struct IsTuple<Tuple, std::tuple<Args...>> : std::true_type {};

struct ConstructibleFromTupleAndInt {
  enum State { FromTuple, FromInt, Copied, Moved };
  State state;

  ConstructibleFromTupleAndInt(ConstructibleFromTupleAndInt const&) : state(Copied) {}
  ConstructibleFromTupleAndInt(ConstructibleFromTupleAndInt &&) : state(Moved) {}

  template <class Tuple, class = typename std::enable_if<IsTuple<Tuple>::value>::type>
  explicit ConstructibleFromTupleAndInt(Tuple&&) : state(FromTuple) {}

  explicit ConstructibleFromTupleAndInt(int) : state(FromInt) {}
};

struct ConvertibleFromTupleAndInt {
  enum State { FromTuple, FromInt, Copied, Moved };
  State state;

  ConvertibleFromTupleAndInt(ConvertibleFromTupleAndInt const&) : state(Copied) {}
  ConvertibleFromTupleAndInt(ConvertibleFromTupleAndInt &&) : state(Moved) {}

  template <class Tuple, class = typename std::enable_if<IsTuple<Tuple>::value>::type>
  ConvertibleFromTupleAndInt(Tuple&&) : state(FromTuple) {}

  ConvertibleFromTupleAndInt(int) : state(FromInt) {}
};

struct ConstructibleFromInt {
  enum State { FromInt, Copied, Moved };
  State state;

  ConstructibleFromInt(ConstructibleFromInt const&) : state(Copied) {}
  ConstructibleFromInt(ConstructibleFromInt &&) : state(Moved) {}

  explicit ConstructibleFromInt(int) : state(FromInt) {}
};

struct ConvertibleFromInt {
  enum State { FromInt, Copied, Moved };
  State state;

  ConvertibleFromInt(ConvertibleFromInt const&) : state(Copied) {}
  ConvertibleFromInt(ConvertibleFromInt &&) : state(Moved) {}
  ConvertibleFromInt(int) : state(FromInt) {}
};

int main()
{
    // Test for the creation of dangling references when a tuple is used to
    // store a reference to another tuple as its only element.
    // Ex std::tuple<std::tuple<int>&&>.
    // In this case the constructors 1) 'tuple(UTypes&&...)'
    // and 2) 'tuple(TupleLike&&)' need to be manually disambiguated because
    // when both #1 and #2 participate in partial ordering #2 will always
    // be chosen over #1.
    // See PR22806  and LWG issue #2549 for more information.
    // (https://bugs.llvm.org/show_bug.cgi?id=22806)
    using T = std::tuple<int>;
    std::allocator<int> A;
    { // rvalue reference
        T t1(42);
        std::tuple< T&& > t2(std::move(t1));
        assert(&std::get<0>(t2) == &t1);
    }
    { // const lvalue reference
        T t1(42);

        std::tuple< T const & > t2(t1);
        assert(&std::get<0>(t2) == &t1);

        std::tuple< T const & > t3(static_cast<T const&>(t1));
        assert(&std::get<0>(t3) == &t1);
    }
    { // lvalue reference
        T t1(42);

        std::tuple< T & > t2(t1);
        assert(&std::get<0>(t2) == &t1);
    }
    { // const rvalue reference
        T t1(42);

        std::tuple< T const && > t2(std::move(t1));
        assert(&std::get<0>(t2) == &t1);
    }
    { // rvalue reference via uses-allocator
        T t1(42);
        std::tuple< T&& > t2(std::allocator_arg, A, std::move(t1));
        assert(&std::get<0>(t2) == &t1);
    }
    { // const lvalue reference via uses-allocator
        T t1(42);

        std::tuple< T const & > t2(std::allocator_arg, A, t1);
        assert(&std::get<0>(t2) == &t1);

        std::tuple< T const & > t3(std::allocator_arg, A, static_cast<T const&>(t1));
        assert(&std::get<0>(t3) == &t1);
    }
    { // lvalue reference via uses-allocator
        T t1(42);

        std::tuple< T & > t2(std::allocator_arg, A, t1);
        assert(&std::get<0>(t2) == &t1);
    }
    { // const rvalue reference via uses-allocator
        T const t1(42);
        std::tuple< T const && > t2(std::allocator_arg, A, std::move(t1));
        assert(&std::get<0>(t2) == &t1);
    }
    // Test constructing a 1-tuple of the form tuple<UDT> from another 1-tuple
    // 'tuple<T>' where UDT *can* be constructed from 'tuple<T>'. In this case
    // the 'tuple(UTypes...)' ctor should be chosen and 'UDT' constructed from
    // 'tuple<T>'.
    {
        using VT = ConstructibleFromTupleAndInt;
        std::tuple<int> t1(42);
        std::tuple<VT> t2(t1);
        assert(std::get<0>(t2).state == VT::FromTuple);
    }
    {
        using VT = ConvertibleFromTupleAndInt;
        std::tuple<int> t1(42);
        std::tuple<VT> t2 = {t1};
        assert(std::get<0>(t2).state == VT::FromTuple);
    }
    // Test constructing a 1-tuple of the form tuple<UDT> from another 1-tuple
    // 'tuple<T>' where UDT cannot be constructed from 'tuple<T>' but can
    // be constructed from 'T'. In this case the tuple-like ctor should be
    // chosen and 'UDT' constructed from 'T'
    {
        using VT = ConstructibleFromInt;
        std::tuple<int> t1(42);
        std::tuple<VT> t2(t1);
        assert(std::get<0>(t2).state == VT::FromInt);
    }
    {
        using VT = ConvertibleFromInt;
        std::tuple<int> t1(42);
        std::tuple<VT> t2 = {t1};
        assert(std::get<0>(t2).state == VT::FromInt);
    }
}