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 // pair(const T1& x, const T2& y); 17 18 #include <utility> 19 #include <cassert> 20 21 #include "archetypes.hpp" 22 #include "test_convertible.hpp" 23 using namespace ImplicitTypes; // Get implicitly archetypes 24 25 struct ExplicitT { 26 constexpr explicit ExplicitT(int x) : value(x) {} 27 constexpr explicit ExplicitT(ExplicitT const& o) : value(o.value) {} 28 int value; 29 }; 30 31 struct ImplicitT { 32 constexpr ImplicitT(int x) : value(x) {} 33 constexpr ImplicitT(ImplicitT const& o) : value(o.value) {} 34 int value; 35 }; 36 37 template <class T1, 38 bool CanCopy = true, bool CanConvert = CanCopy> 39 void test_sfinae() { 40 using P1 = std::pair<T1, int>; 41 using P2 = std::pair<int, T1>; 42 using T1Arg = T1 const&; 43 using T2 = int const&; 44 static_assert(std::is_constructible<P1, T1Arg, T2>::value == CanCopy, ""); 45 static_assert(test_convertible<P1, T1Arg, T2>() == CanConvert, ""); 46 static_assert(std::is_constructible<P2, T2, T1Arg>::value == CanCopy, ""); 47 static_assert(test_convertible<P2, T2, T1Arg>() == CanConvert, ""); 48 } 49 50 int main() 51 { 52 { 53 typedef std::pair<float, short*> P; 54 P p(3.5f, 0); 55 assert(p.first == 3.5f); 56 assert(p.second == nullptr); 57 } 58 { 59 typedef std::pair<ImplicitT, int> P; 60 P p(1, 2); 61 assert(p.first.value == 1); 62 assert(p.second == 2); 63 } 64 { 65 test_sfinae<AllCtors>(); 66 test_sfinae<ExplicitTypes::AllCtors, true, false>(); 67 test_sfinae<CopyOnly>(); 68 test_sfinae<ExplicitTypes::CopyOnly, true, false>(); 69 test_sfinae<MoveOnly, false>(); 70 test_sfinae<ExplicitTypes::MoveOnly, false>(); 71 test_sfinae<NonCopyable, false>(); 72 test_sfinae<ExplicitTypes::NonCopyable, false>(); 73 } 74 #if TEST_STD_VER > 11 75 { 76 typedef std::pair<float, short*> P; 77 constexpr P p(3.5f, 0); 78 static_assert(p.first == 3.5f, ""); 79 static_assert(p.second == nullptr, ""); 80 } 81 { 82 using P = std::pair<ExplicitT, int>; 83 constexpr ExplicitT e(42); 84 constexpr int x = 10; 85 constexpr P p(e, x); 86 static_assert(p.first.value == 42, ""); 87 static_assert(p.second == 10, ""); 88 } 89 { 90 using P = std::pair<ImplicitT, int>; 91 constexpr ImplicitT e(42); 92 constexpr int x = 10; 93 constexpr P p = {e, x}; 94 static_assert(p.first.value == 42, ""); 95 static_assert(p.second == 10, ""); 96 } 97 #endif 98 } 99