-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariant_base.h
68 lines (52 loc) · 1.71 KB
/
variant_base.h
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
#include "variant_storage.h"
template <bool Tr, typename... Args>
struct base {
std::size_t m_index = 0;
storage<false, Args...> m_storage;
constexpr base(empty_class e) : m_storage(e), m_index(0) {}
template <std::size_t I, typename... Types>
constexpr void put(in_place_index_t<I>, Types&&... types) {
m_storage.put(in_place_index<I>, std::forward<Types>(types)...);
}
template <std::size_t I, typename... Types>
constexpr base(in_place_index_t<I>, Types&&... types)
: m_storage(in_place_index<I>, std::forward<Types>(types)...), m_index(I) {}
constexpr bool valueless_by_exception() const noexcept {
return m_index == variant_npos;
}
constexpr std::size_t index() const noexcept {
return m_index;
}
constexpr void destroy() {
if (m_index != variant_npos) {
m_storage.destroy(m_index);
}
m_index = variant_npos;
}
constexpr ~base() {
destroy();
}
};
template <typename... Args>
struct base<true, Args...> {
std::size_t m_index = 0;
storage<true, Args...> m_storage;
constexpr base(empty_class e) : m_storage(e), m_index(0) {}
template <std::size_t I, typename... Types>
constexpr base(in_place_index_t<I>, Types&&... types)
: m_storage(in_place_index<I>, std::forward<Types>(types)...), m_index(I) {}
template <std::size_t I, typename... Types>
constexpr void put(in_place_index_t<I>, Types&&... types) {
m_storage.put(in_place_index<I>, std::forward<Types>(types)...);
}
constexpr bool valueless_by_exception() const noexcept {
return m_index == variant_npos;
}
constexpr std::size_t index() const noexcept {
return m_index;
}
constexpr void destroy() {
m_index = variant_npos;
}
constexpr ~base() = default;
};