1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // UNSUPPORTED: c++98, c++03
11
12 // <utility>
13
14 // template <class T1, class T2> struct pair
15
16 // template<class U, class V> pair(U&& x, V&& y);
17
18
19 #include <utility>
20 #include <memory>
21 #include <cassert>
22
23 #include "archetypes.hpp"
24 #include "test_convertible.hpp"
25 using namespace ImplicitTypes; // Get implicitly archetypes
26
27 template <class T1, class T1Arg,
28 bool CanCopy = true, bool CanConvert = CanCopy>
test_sfinae()29 void test_sfinae() {
30 using P1 = std::pair<T1, int>;
31 using P2 = std::pair<int, T1>;
32 using T2 = int const&;
33 static_assert(std::is_constructible<P1, T1Arg, T2>::value == CanCopy, "");
34 static_assert(test_convertible<P1, T1Arg, T2>() == CanConvert, "");
35 static_assert(std::is_constructible<P2, T2, T1Arg>::value == CanCopy, "");
36 static_assert(test_convertible<P2, T2, T1Arg>() == CanConvert, "");
37 }
38
39 struct ExplicitT {
ExplicitTExplicitT40 constexpr explicit ExplicitT(int x) : value(x) {}
41 int value;
42 };
43
44 struct ImplicitT {
ImplicitTImplicitT45 constexpr ImplicitT(int x) : value(x) {}
46 int value;
47 };
48
49
main()50 int main()
51 {
52 {
53 typedef std::pair<std::unique_ptr<int>, short*> P;
54 P p(std::unique_ptr<int>(new int(3)), nullptr);
55 assert(*p.first == 3);
56 assert(p.second == nullptr);
57 }
58 {
59 // Test non-const lvalue and rvalue types
60 test_sfinae<AllCtors, AllCtors&>();
61 test_sfinae<AllCtors, AllCtors&&>();
62 test_sfinae<ExplicitTypes::AllCtors, ExplicitTypes::AllCtors&, true, false>();
63 test_sfinae<ExplicitTypes::AllCtors, ExplicitTypes::AllCtors&&, true, false>();
64 test_sfinae<CopyOnly, CopyOnly&>();
65 test_sfinae<CopyOnly, CopyOnly&&>();
66 test_sfinae<ExplicitTypes::CopyOnly, ExplicitTypes::CopyOnly&, true, false>();
67 test_sfinae<ExplicitTypes::CopyOnly, ExplicitTypes::CopyOnly&&, true, false>();
68 test_sfinae<MoveOnly, MoveOnly&, false>();
69 test_sfinae<MoveOnly, MoveOnly&&>();
70 test_sfinae<ExplicitTypes::MoveOnly, ExplicitTypes::MoveOnly&, false>();
71 test_sfinae<ExplicitTypes::MoveOnly, ExplicitTypes::MoveOnly&&, true, false>();
72 test_sfinae<NonCopyable, NonCopyable&, false>();
73 test_sfinae<NonCopyable, NonCopyable&&, false>();
74 test_sfinae<ExplicitTypes::NonCopyable, ExplicitTypes::NonCopyable&, false>();
75 test_sfinae<ExplicitTypes::NonCopyable, ExplicitTypes::NonCopyable&&, false>();
76 }
77 {
78 // Test converting types
79 test_sfinae<ConvertingType, int&>();
80 test_sfinae<ConvertingType, const int&>();
81 test_sfinae<ConvertingType, int&&>();
82 test_sfinae<ConvertingType, const int&&>();
83 test_sfinae<ExplicitTypes::ConvertingType, int&, true, false>();
84 test_sfinae<ExplicitTypes::ConvertingType, const int&, true, false>();
85 test_sfinae<ExplicitTypes::ConvertingType, int&&, true, false>();
86 test_sfinae<ExplicitTypes::ConvertingType, const int&&, true, false>();
87 }
88 #if TEST_STD_VER > 11
89 { // explicit constexpr test
90 constexpr std::pair<ExplicitT, ExplicitT> p(42, 43);
91 static_assert(p.first.value == 42, "");
92 static_assert(p.second.value == 43, "");
93 }
94 { // implicit constexpr test
95 constexpr std::pair<ImplicitT, ImplicitT> p = {42, 43};
96 static_assert(p.first.value == 42, "");
97 static_assert(p.second.value == 43, "");
98 }
99 #endif
100 }
101