• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
11 
12 #include <utility>
13 #include <string>
14 #include <type_traits>
15 #include <complex>
16 #include <memory>
17 
18 #include <cassert>
19 
main()20 int main()
21 {
22     typedef std::complex<float> cf;
23     {
24     auto t1 = std::make_pair<int, cf> ( 42, { 1,2 } );
25     assert ( std::get<int>(t1) == 42 );
26     assert ( std::get<cf>(t1).real() == 1 );
27     assert ( std::get<cf>(t1).imag() == 2 );
28     }
29 
30     {
31     const std::pair<int, const int> p1 { 1, 2 };
32     const int &i1 = std::get<int>(p1);
33     const int &i2 = std::get<const int>(p1);
34     assert ( i1 == 1 );
35     assert ( i2 == 2 );
36     }
37 
38     {
39     typedef std::unique_ptr<int> upint;
40     std::pair<upint, int> t(upint(new int(4)), 42);
41     upint p = std::get<upint>(std::move(t)); // get rvalue
42     assert(*p == 4);
43     assert(std::get<upint>(t) == nullptr); // has been moved from
44     }
45 
46     {
47     typedef std::unique_ptr<int> upint;
48     const std::pair<upint, int> t(upint(new int(4)), 42);
49     static_assert(std::is_same<const upint&&, decltype(std::get<upint>(std::move(t)))>::value, "");
50     static_assert(noexcept(std::get<upint>(std::move(t))), "");
51     static_assert(std::is_same<const int&&, decltype(std::get<int>(std::move(t)))>::value, "");
52     static_assert(noexcept(std::get<int>(std::move(t))), "");
53     auto&& p = std::get<upint>(std::move(t)); // get const rvalue
54     auto&& i = std::get<int>(std::move(t)); // get const rvalue
55     assert(*p == 4);
56     assert(i == 42);
57     assert(std::get<upint>(t) != nullptr);
58     }
59 
60     {
61     int x = 42;
62     int const y = 43;
63     std::pair<int&, int const&> const p(x, y);
64     static_assert(std::is_same<int&, decltype(std::get<int&>(std::move(p)))>::value, "");
65     static_assert(noexcept(std::get<int&>(std::move(p))), "");
66     static_assert(std::is_same<int const&, decltype(std::get<int const&>(std::move(p)))>::value, "");
67     static_assert(noexcept(std::get<int const&>(std::move(p))), "");
68     }
69 
70     {
71     int x = 42;
72     int const y = 43;
73     std::pair<int&&, int const&&> const p(std::move(x), std::move(y));
74     static_assert(std::is_same<int&&, decltype(std::get<int&&>(std::move(p)))>::value, "");
75     static_assert(noexcept(std::get<int&&>(std::move(p))), "");
76     static_assert(std::is_same<int const&&, decltype(std::get<int const&&>(std::move(p)))>::value, "");
77     static_assert(noexcept(std::get<int const&&>(std::move(p))), "");
78     }
79 
80     {
81     constexpr const std::pair<int, const int> p { 1, 2 };
82     static_assert(std::get<int>(std::move(p)) == 1, "");
83     static_assert(std::get<const int>(std::move(p)) == 2, "");
84     }
85 }
86