Home | History | Annotate | Download | only in pairs.pair
      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, c++11, c++14
     11 // UNSUPPORTED: libcpp-no-deduction-guides
     12 
     13 // GCC's implementation of class template deduction is still immature and runs
     14 // into issues with libc++. However GCC accepts this code when compiling
     15 // against libstdc++.
     16 // XFAIL: gcc
     17 
     18 // <utility>
     19 
     20 // Test that the constructors offered by std::pair are formulated
     21 // so they're compatible with implicit deduction guides, or if that's not
     22 // possible that they provide explicit guides to make it work.
     23 
     24 #include <utility>
     25 #include <memory>
     26 #include <string>
     27 #include <cassert>
     28 
     29 #include "test_macros.h"
     30 #include "archetypes.hpp"
     31 
     32 
     33 // Overloads
     34 // ---------------
     35 // (1)  pair(const T1&, const T2&) -> pair<T1, T2>
     36 // (2)  explicit pair(const T1&, const T2&) -> pair<T1, T2>
     37 // (3)  pair(pair const& t) -> decltype(t)
     38 // (4)  pair(pair&& t) -> decltype(t)
     39 // (5)  pair(pair<U1, U2> const&) -> pair<U1, U2>
     40 // (6)  explicit pair(pair<U1, U2> const&) -> pair<U1, U2>
     41 // (7)  pair(pair<U1, U2> &&) -> pair<U1, U2>
     42 // (8)  explicit pair(pair<U1, U2> &&) -> pair<U1, U2>
     43 int main()
     44 {
     45   using E = ExplicitTestTypes::TestType;
     46   static_assert(!std::is_convertible<E const&, E>::value, "");
     47   { // Testing (1)
     48     int const x = 42;
     49     std::pair t1("abc", x);
     50     ASSERT_SAME_TYPE(decltype(t1), std::pair<const char*, int>);
     51   }
     52   { // Testing (2)
     53     std::pair p1(E{}, 42);
     54     ASSERT_SAME_TYPE(decltype(p1), std::pair<E, int>);
     55 
     56     const E t{};
     57     std::pair p2(t, E{});
     58     ASSERT_SAME_TYPE(decltype(p2), std::pair<E, E>);
     59   }
     60   { // Testing (3, 5)
     61     std::pair<double, decltype(nullptr)> const p(0.0, nullptr);
     62     std::pair p1(p);
     63     ASSERT_SAME_TYPE(decltype(p1), std::pair<double, decltype(nullptr)>);
     64   }
     65   { // Testing (3, 6)
     66     std::pair<E, decltype(nullptr)> const p(E{}, nullptr);
     67     std::pair p1(p);
     68     ASSERT_SAME_TYPE(decltype(p1), std::pair<E, decltype(nullptr)>);
     69   }
     70   { // Testing (4, 7)
     71     std::pair<std::string, void*> p("abc", nullptr);
     72     std::pair p1(std::move(p));
     73     ASSERT_SAME_TYPE(decltype(p1), std::pair<std::string, void*>);
     74   }
     75   { // Testing (4, 8)
     76     std::pair<std::string, E> p("abc", E{});
     77     std::pair p1(std::move(p));
     78     ASSERT_SAME_TYPE(decltype(p1), std::pair<std::string, E>);
     79   }
     80 }
     81