aboutsummaryrefslogtreecommitdiff
path: root/test/std/utilities/utility/pairs/pairs.pair/rv_pair_U_V.pass.cpp
blob: f5d3bb621debcc4aa0be9f940e5d4e00a95e2618 (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
//===----------------------------------------------------------------------===//
//
//                     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

// <utility>

// template <class T1, class T2> struct pair

// template <class U, class V> pair(pair<U, V>&& p);

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

#include "archetypes.hpp"
#include "test_convertible.hpp"
using namespace ImplicitTypes; // Get implicitly archetypes

template <class T1, class U1,
          bool CanCopy = true, bool CanConvert = CanCopy>
void test_pair_rv()
{
    using P1 = std::pair<T1, int>;
    using P2 = std::pair<int, T1>;
    using UP1 = std::pair<U1, int>&&;
    using UP2 = std::pair<int, U1>&&;
    static_assert(std::is_constructible<P1, UP1>::value == CanCopy, "");
    static_assert(test_convertible<P1, UP1>() == CanConvert, "");
    static_assert(std::is_constructible<P2, UP2>::value == CanCopy, "");
    static_assert(test_convertible<P2,  UP2>() == CanConvert, "");
}

struct Base
{
    virtual ~Base() {}
};

struct Derived
    : public Base
{
};


template <class T, class U>
struct DPair : public std::pair<T, U> {
  using Base = std::pair<T, U>;
  using Base::Base;
};

struct ExplicitT {
  constexpr explicit ExplicitT(int x) : value(x) {}
  int value;
};

struct ImplicitT {
  constexpr ImplicitT(int x) : value(x) {}
  int value;
};

int main()
{
    {
        typedef std::pair<std::unique_ptr<Derived>, short> P1;
        typedef std::pair<std::unique_ptr<Base>, long> P2;
        P1 p1(std::unique_ptr<Derived>(), 4);
        P2 p2 = std::move(p1);
        assert(p2.first == nullptr);
        assert(p2.second == 4);
    }
    {
        // We allow derived types to use this constructor
        using P1 = DPair<long, long>;
        using P2 = std::pair<int, int>;
        P1 p1(42, 101);
        P2 p2(std::move(p1));
        assert(p2.first == 42);
        assert(p2.second == 101);
    }
    {
        test_pair_rv<AllCtors, AllCtors>();
        test_pair_rv<AllCtors, AllCtors&>();
        test_pair_rv<AllCtors, AllCtors&&>();
        test_pair_rv<AllCtors, const AllCtors&>();
        test_pair_rv<AllCtors, const AllCtors&&>();

        test_pair_rv<ExplicitTypes::AllCtors, ExplicitTypes::AllCtors>();
        test_pair_rv<ExplicitTypes::AllCtors, ExplicitTypes::AllCtors&, true, false>();
        test_pair_rv<ExplicitTypes::AllCtors, ExplicitTypes::AllCtors&&, true, false>();
        test_pair_rv<ExplicitTypes::AllCtors, const ExplicitTypes::AllCtors&, true, false>();
        test_pair_rv<ExplicitTypes::AllCtors, const ExplicitTypes::AllCtors&&, true, false>();

        test_pair_rv<MoveOnly, MoveOnly>();
        test_pair_rv<MoveOnly, MoveOnly&, false>();
        test_pair_rv<MoveOnly, MoveOnly&&>();

        test_pair_rv<ExplicitTypes::MoveOnly, ExplicitTypes::MoveOnly>(); // copy construction
        test_pair_rv<ExplicitTypes::MoveOnly, ExplicitTypes::MoveOnly&, false>();
        test_pair_rv<ExplicitTypes::MoveOnly, ExplicitTypes::MoveOnly&&, true, false>();

        test_pair_rv<CopyOnly, CopyOnly>();
        test_pair_rv<CopyOnly, CopyOnly&>();
        test_pair_rv<CopyOnly, CopyOnly&&>();

        test_pair_rv<ExplicitTypes::CopyOnly, ExplicitTypes::CopyOnly>();
        test_pair_rv<ExplicitTypes::CopyOnly, ExplicitTypes::CopyOnly&, true, false>();
        test_pair_rv<ExplicitTypes::CopyOnly, ExplicitTypes::CopyOnly&&, true, false>();

        test_pair_rv<NonCopyable, NonCopyable, false>();
        test_pair_rv<NonCopyable, NonCopyable&, false>();
        test_pair_rv<NonCopyable, NonCopyable&&, false>();
        test_pair_rv<NonCopyable, const NonCopyable&, false>();
        test_pair_rv<NonCopyable, const NonCopyable&&, false>();
    }
    { // Test construction of references
        test_pair_rv<NonCopyable&, NonCopyable&>();
        test_pair_rv<NonCopyable&, NonCopyable&&>();
        test_pair_rv<NonCopyable&, NonCopyable const&, false>();
        test_pair_rv<NonCopyable const&, NonCopyable&&>();
        test_pair_rv<NonCopyable&&, NonCopyable&&>();

        test_pair_rv<ConvertingType&, int, false>();
        test_pair_rv<ExplicitTypes::ConvertingType&, int, false>();
        // Unfortunately the below conversions are allowed and create dangling
        // references.
        //test_pair_rv<ConvertingType&&, int>();
        //test_pair_rv<ConvertingType const&, int>();
        //test_pair_rv<ConvertingType const&&, int>();
        // But these are not because the converting constructor is explicit.
        test_pair_rv<ExplicitTypes::ConvertingType&&, int, false>();
        test_pair_rv<ExplicitTypes::ConvertingType const&, int, false>();
        test_pair_rv<ExplicitTypes::ConvertingType const&&, int, false>();
    }
    {
        test_pair_rv<AllCtors, int, false>();
        test_pair_rv<ExplicitTypes::AllCtors, int, false>();
        test_pair_rv<ConvertingType, int>();
        test_pair_rv<ExplicitTypes::ConvertingType, int, true, false>();

        test_pair_rv<ConvertingType, int>();
        test_pair_rv<ConvertingType, ConvertingType>();
        test_pair_rv<ConvertingType, ConvertingType const&>();
        test_pair_rv<ConvertingType, ConvertingType&>();
        test_pair_rv<ConvertingType, ConvertingType&&>();

        test_pair_rv<ExplicitTypes::ConvertingType, int, true, false>();
        test_pair_rv<ExplicitTypes::ConvertingType, int&, true, false>();
        test_pair_rv<ExplicitTypes::ConvertingType, const int&, true, false>();
        test_pair_rv<ExplicitTypes::ConvertingType, int&&, true, false>();
        test_pair_rv<ExplicitTypes::ConvertingType, const int&&, true, false>();

        test_pair_rv<ExplicitTypes::ConvertingType, ExplicitTypes::ConvertingType>();
        test_pair_rv<ExplicitTypes::ConvertingType, ExplicitTypes::ConvertingType const&, true, false>();
        test_pair_rv<ExplicitTypes::ConvertingType, ExplicitTypes::ConvertingType&, true, false>();
        test_pair_rv<ExplicitTypes::ConvertingType, ExplicitTypes::ConvertingType&&, true, false>();
    }
#if TEST_STD_VER > 11
    { // explicit constexpr test
        constexpr std::pair<int, int> p1(42, 43);
        constexpr std::pair<ExplicitT, ExplicitT> p2(std::move(p1));
        static_assert(p2.first.value == 42, "");
        static_assert(p2.second.value == 43, "");
    }
    { // implicit constexpr test
        constexpr std::pair<int, int> p1(42, 43);
        constexpr std::pair<ImplicitT, ImplicitT> p2 = std::move(p1);
        static_assert(p2.first.value == 42, "");
        static_assert(p2.second.value == 43, "");
    }
#endif
}