• 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 // <tuple>
10 
11 // template <class... Types> class tuple;
12 
13 // template <class... UTypes>
14 //   tuple& operator=(tuple<UTypes...>&& u);
15 
16 // UNSUPPORTED: c++03
17 
18 #include <tuple>
19 #include <string>
20 #include <memory>
21 #include <utility>
22 #include <cassert>
23 
24 #include "test_macros.h"
25 
26 struct B
27 {
28     int id_;
29 
BB30     explicit B(int i= 0) : id_(i) {}
31 
~BB32     virtual ~B() {}
33 };
34 
35 struct D
36     : B
37 {
DD38     explicit D(int i) : B(i) {}
39 };
40 
41 struct E {
42   E() = default;
operator =E43   E& operator=(int) {
44       return *this;
45   }
46 };
47 
main(int,char **)48 int main(int, char**)
49 {
50     {
51         typedef std::tuple<long> T0;
52         typedef std::tuple<long long> T1;
53         T0 t0(2);
54         T1 t1;
55         t1 = std::move(t0);
56         assert(std::get<0>(t1) == 2);
57     }
58     {
59         typedef std::tuple<long, char> T0;
60         typedef std::tuple<long long, int> T1;
61         T0 t0(2, 'a');
62         T1 t1;
63         t1 = std::move(t0);
64         assert(std::get<0>(t1) == 2);
65         assert(std::get<1>(t1) == int('a'));
66     }
67     {
68         typedef std::tuple<long, char, D> T0;
69         typedef std::tuple<long long, int, B> T1;
70         T0 t0(2, 'a', D(3));
71         T1 t1;
72         t1 = std::move(t0);
73         assert(std::get<0>(t1) == 2);
74         assert(std::get<1>(t1) == int('a'));
75         assert(std::get<2>(t1).id_ == 3);
76     }
77     {
78         D d(3);
79         D d2(2);
80         typedef std::tuple<long, char, D&> T0;
81         typedef std::tuple<long long, int, B&> T1;
82         T0 t0(2, 'a', d2);
83         T1 t1(1, 'b', d);
84         t1 = std::move(t0);
85         assert(std::get<0>(t1) == 2);
86         assert(std::get<1>(t1) == int('a'));
87         assert(std::get<2>(t1).id_ == 2);
88     }
89     {
90         typedef std::tuple<long, char, std::unique_ptr<D>> T0;
91         typedef std::tuple<long long, int, std::unique_ptr<B>> T1;
92         T0 t0(2, 'a', std::unique_ptr<D>(new D(3)));
93         T1 t1;
94         t1 = std::move(t0);
95         assert(std::get<0>(t1) == 2);
96         assert(std::get<1>(t1) == int('a'));
97         assert(std::get<2>(t1)->id_ == 3);
98     }
99     {
100         // Test that tuple evaluates correctly applies an lvalue reference
101         // before evaluating is_assignable (i.e. 'is_assignable<int&, int&&>')
102         // instead of evaluating 'is_assignable<int&&, int&&>' which is false.
103         int x = 42;
104         int y = 43;
105         std::tuple<int&&, E> t(std::move(x), E{});
106         std::tuple<int&&, int> t2(std::move(y), 44);
107         t = std::move(t2);
108         assert(std::get<0>(t) == 43);
109         assert(&std::get<0>(t) == &x);
110     }
111 
112   return 0;
113 }
114