• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03
10 
11 // <utility>
12 
13 // template <class T1, class T2> struct pair
14 
15 // template<class U, class V> pair(U&& x, V&& y);
16 
17 
18 #include <utility>
19 #include <memory>
20 #include <cassert>
21 
22 #include "archetypes.h"
23 #include "test_convertible.h"
24 
25 #include "test_macros.h"
26 using namespace ImplicitTypes; // Get implicitly archetypes
27 
28 template <class T1, class T1Arg,
29           bool CanCopy = true, bool CanConvert = CanCopy>
test_sfinae()30 void test_sfinae() {
31     using P1 = std::pair<T1, int>;
32     using P2 = std::pair<int, T1>;
33     using T2 = int const&;
34     static_assert(std::is_constructible<P1, T1Arg, T2>::value == CanCopy, "");
35     static_assert(test_convertible<P1,   T1Arg, T2>() == CanConvert, "");
36     static_assert(std::is_constructible<P2, T2,   T1Arg>::value == CanCopy, "");
37     static_assert(test_convertible<P2,   T2,   T1Arg>() == CanConvert, "");
38 }
39 
40 struct ExplicitT {
ExplicitTExplicitT41   constexpr explicit ExplicitT(int x) : value(x) {}
42   int value;
43 };
44 
45 struct ImplicitT {
ImplicitTImplicitT46   constexpr ImplicitT(int x) : value(x) {}
47   int value;
48 };
49 
50 
main(int,char **)51 int main(int, char**)
52 {
53     {
54         typedef std::pair<std::unique_ptr<int>, short*> P;
55         P p(std::unique_ptr<int>(new int(3)), nullptr);
56         assert(*p.first == 3);
57         assert(p.second == nullptr);
58     }
59     {
60         // Test non-const lvalue and rvalue types
61         test_sfinae<AllCtors, AllCtors&>();
62         test_sfinae<AllCtors, AllCtors&&>();
63         test_sfinae<ExplicitTypes::AllCtors, ExplicitTypes::AllCtors&, true, false>();
64         test_sfinae<ExplicitTypes::AllCtors, ExplicitTypes::AllCtors&&, true, false>();
65         test_sfinae<CopyOnly, CopyOnly&>();
66         test_sfinae<CopyOnly, CopyOnly&&>();
67         test_sfinae<ExplicitTypes::CopyOnly, ExplicitTypes::CopyOnly&, true, false>();
68         test_sfinae<ExplicitTypes::CopyOnly, ExplicitTypes::CopyOnly&&, true, false>();
69         test_sfinae<MoveOnly, MoveOnly&, false>();
70         test_sfinae<MoveOnly, MoveOnly&&>();
71         test_sfinae<ExplicitTypes::MoveOnly, ExplicitTypes::MoveOnly&, false>();
72         test_sfinae<ExplicitTypes::MoveOnly, ExplicitTypes::MoveOnly&&, true, false>();
73         test_sfinae<NonCopyable, NonCopyable&, false>();
74         test_sfinae<NonCopyable, NonCopyable&&, false>();
75         test_sfinae<ExplicitTypes::NonCopyable, ExplicitTypes::NonCopyable&, false>();
76         test_sfinae<ExplicitTypes::NonCopyable, ExplicitTypes::NonCopyable&&, false>();
77     }
78     {
79         // Test converting types
80         test_sfinae<ConvertingType, int&>();
81         test_sfinae<ConvertingType, const int&>();
82         test_sfinae<ConvertingType, int&&>();
83         test_sfinae<ConvertingType, const int&&>();
84         test_sfinae<ExplicitTypes::ConvertingType, int&, true, false>();
85         test_sfinae<ExplicitTypes::ConvertingType, const int&, true, false>();
86         test_sfinae<ExplicitTypes::ConvertingType, int&&, true, false>();
87         test_sfinae<ExplicitTypes::ConvertingType, const int&&, true, false>();
88     }
89 #if TEST_STD_VER > 11
90     { // explicit constexpr test
91         constexpr std::pair<ExplicitT, ExplicitT> p(42, 43);
92         static_assert(p.first.value == 42, "");
93         static_assert(p.second.value == 43, "");
94     }
95     { // implicit constexpr test
96         constexpr std::pair<ImplicitT, ImplicitT> p = {42, 43};
97         static_assert(p.first.value == 42, "");
98         static_assert(p.second.value == 43, "");
99     }
100 #endif
101 
102   return 0;
103 }
104