-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyTuple.cpp
More file actions
96 lines (84 loc) · 2.33 KB
/
myTuple.cpp
File metadata and controls
96 lines (84 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <string>
class A {
public:
A() {
std::cout << "A cosntructor" << std::endl;
}
A(const A& other) {
std::cout << "A copy constructor" << std::endl;
}
A(A&& other) {
std::cout << "move cosntructor" << std::endl;
}
A& operator=(A&& other) {
std::cout << "move assignment" << std::endl;
return *this;
}
A& operator=(const A& other) {
std::cout << "copy assignment" << std::endl;
return *this;
}
~A() {
std::cout << "A Destructirr" << std::endl;
};
};
template <typename... Ts>
class Tuple;
template <>
class Tuple<> {};
template <typename T, typename... Ts>
class Tuple<T, Ts...> : private Tuple<Ts...> {
public:
Tuple() = default;
Tuple(T x, Ts... args) {
value = x;
rest = Tuple<std::decay_t<Ts>...>(std::forward<Ts>(args)...);
}
Tuple(const Tuple& other) {
value = other.value;
rest = other.rest;
}
Tuple& operator=(Tuple&& other) {
if (&other != this) {
value = std::move(other.value);
rest = std::move(other.rest);
}
return *this;
}
Tuple& operator=(const Tuple& other) {
if (&other != this) {
value = other.value;
rest = other.rest;
}
return *this;
}
~Tuple() = default;
T value;
Tuple<Ts...> rest;
};
template <size_t N, typename... Ts, template <typename...> class Tuple>
constexpr auto get(Tuple<Ts...>& tuple) {
if constexpr (N == 0) {
return tuple.value;
}
else {
return get<N - 1>(tuple.rest);
}
}
template <typename T>
struct tuple_size;
template <typename... Ts>
struct tuple_size<Tuple<Ts...>> : std::integral_constant<std::size_t, sizeof...(Ts)> {};
template <typename T>
constexpr std::size_t tuple_size_v = tuple_size<T>::value;
template <typename... Ts>
Tuple(Ts...) -> Tuple<Ts...>;
int main() {
A a{};
Tuple<int, double, A> t{3, 2.0, a};
Tuple s = std::move(t);
std::cout << "hi" << std::endl;
std::cout << get<1>(t) << std::endl;
std::cout << tuple_size_v<decltype(s)> << std::endl;
}