File: move_pair.pass.cpp

package info (click to toggle)
llvm-toolchain-13 1%3A13.0.1-6~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,418,812 kB
  • sloc: cpp: 5,290,827; ansic: 996,570; asm: 544,593; python: 188,212; objc: 72,027; lisp: 30,291; f90: 25,395; sh: 24,900; javascript: 9,780; pascal: 9,398; perl: 7,484; ml: 5,432; awk: 3,523; makefile: 2,892; xml: 953; cs: 573; fortran: 539
file content (204 lines) | stat: -rw-r--r-- 6,421 bytes parent folder | download | duplicates (20)
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
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// <tuple>

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

// template <class U1, class U2>
//   tuple& operator=(pair<U1, U2>&& u);

// UNSUPPORTED: c++03

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

#include "test_macros.h"

struct B
{
    int id_;

    explicit B(int i = 0) : id_(i) {}

    virtual ~B() {}
};

struct D
    : B
{
    explicit D(int i) : B(i) {}
};

struct TrackMove
{
    TrackMove() : value(0), moved_from(false) { }
    explicit TrackMove(int v) : value(v), moved_from(false) { }
    TrackMove(TrackMove const& other) : value(other.value), moved_from(false) { }
    TrackMove(TrackMove&& other) : value(other.value), moved_from(false) {
        other.moved_from = true;
    }
    TrackMove& operator=(TrackMove const& other) {
        value = other.value;
        moved_from = false;
        return *this;
    }
    TrackMove& operator=(TrackMove&& other) {
        value = other.value;
        moved_from = false;
        other.moved_from = true;
        return *this;
    }

    int value;
    bool moved_from;
};

struct NonAssignable
{
  NonAssignable& operator=(NonAssignable const&) = delete;
  NonAssignable& operator=(NonAssignable&&) = delete;
};

struct MoveAssignable
{
  MoveAssignable& operator=(MoveAssignable const&) = delete;
  MoveAssignable& operator=(MoveAssignable&&) = default;
};

struct CopyAssignable
{
  CopyAssignable& operator=(CopyAssignable const&) = default;
  CopyAssignable& operator=(CopyAssignable&&) = delete;
};

struct NothrowMoveAssignable
{
    NothrowMoveAssignable& operator=(NothrowMoveAssignable&&) noexcept { return *this; }
};

struct PotentiallyThrowingMoveAssignable
{
    PotentiallyThrowingMoveAssignable& operator=(PotentiallyThrowingMoveAssignable&&) { return *this; }
};

int main(int, char**)
{
    {
        typedef std::pair<long, std::unique_ptr<D>> T0;
        typedef std::tuple<long long, std::unique_ptr<B>> T1;
        T0 t0(2, std::unique_ptr<D>(new D(3)));
        T1 t1;
        t1 = std::move(t0);
        assert(std::get<0>(t1) == 2);
        assert(std::get<1>(t1)->id_ == 3);
    }
    {
        using T = std::tuple<int, NonAssignable>;
        using P = std::pair<int, NonAssignable>;
        static_assert(!std::is_assignable<T&, P&&>::value, "");
    }
    {
      using T = std::tuple<int, int, int>;
      using P = std::pair<int, int>;
      static_assert(!std::is_assignable<T&, P&&>::value, "");
    }
    {
        typedef std::tuple<NothrowMoveAssignable, long> Tuple;
        typedef std::pair<NothrowMoveAssignable, int> Pair;
        static_assert(std::is_nothrow_assignable<Tuple&, Pair&&>::value, "");
        static_assert(!std::is_assignable<Tuple&, Pair const&&>::value, "");
    }
    {
        typedef std::tuple<PotentiallyThrowingMoveAssignable, long> Tuple;
        typedef std::pair<PotentiallyThrowingMoveAssignable, int> Pair;
        static_assert(std::is_assignable<Tuple&, Pair&&>::value, "");
        static_assert(!std::is_nothrow_assignable<Tuple&, Pair&&>::value, "");
        static_assert(!std::is_assignable<Tuple&, Pair const&&>::value, "");
    }
    {
        // We assign through the reference and don't move out of the incoming ref,
        // so this doesn't work (but would if the type were CopyAssignable).
        {
            using T = std::tuple<MoveAssignable&, int>;
            using P = std::pair<MoveAssignable&, int>;
            static_assert(!std::is_assignable<T&, P&&>::value, "");
        }

        // ... works if it's CopyAssignable
        {
            using T = std::tuple<CopyAssignable&, int>;
            using P = std::pair<CopyAssignable&, int>;
            static_assert(std::is_assignable<T&, P&&>::value, "");
        }

        // For rvalue-references, we can move-assign if the type is MoveAssignable
        // or CopyAssignable (since in the worst case the move will decay into a copy).
        {
            using T1 = std::tuple<MoveAssignable&&, int>;
            using P1 = std::pair<MoveAssignable&&, int>;
            static_assert(std::is_assignable<T1&, P1&&>::value, "");

            using T2 = std::tuple<CopyAssignable&&, int>;
            using P2 = std::pair<CopyAssignable&&, int>;
            static_assert(std::is_assignable<T2&, P2&&>::value, "");
        }

        // In all cases, we can't move-assign if the types are not assignable,
        // since we assign through the reference.
        {
            using T1 = std::tuple<NonAssignable&, int>;
            using P1 = std::pair<NonAssignable&, int>;
            static_assert(!std::is_assignable<T1&, P1&&>::value, "");

            using T2 = std::tuple<NonAssignable&&, int>;
            using P2 = std::pair<NonAssignable&&, int>;
            static_assert(!std::is_assignable<T2&, P2&&>::value, "");
        }
    }
    {
        // Make sure that we don't incorrectly move out of the source's reference.
        using Dest = std::tuple<TrackMove, int>;
        using Source = std::pair<TrackMove&, int>;
        TrackMove track{3};
        Source src(track, 4);
        assert(!track.moved_from);

        Dest dst;
        dst = std::move(src); // here we should make a copy
        assert(!track.moved_from);
        assert(std::get<0>(dst).value == 3);
    }
    {
        // But we do move out of the source's reference if it's a rvalue ref
        using Dest = std::tuple<TrackMove, int>;
        using Source = std::pair<TrackMove&&, int>;
        TrackMove track{3};
        Source src(std::move(track), 4);
        assert(!track.moved_from); // we just took a reference

        Dest dst;
        dst = std::move(src);
        assert(track.moved_from);
        assert(std::get<0>(dst).value == 3);
    }
    {
        // If the pair holds a value, then we move out of it too
        using Dest = std::tuple<TrackMove, int>;
        using Source = std::pair<TrackMove, int>;
        Source src(TrackMove{3}, 4);
        Dest dst;
        dst = std::move(src);
        assert(src.first.moved_from);
        assert(std::get<0>(dst).value == 3);
    }

    return 0;
}