forked from lefticus/cpp_weekly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathafter.cpp
More file actions
34 lines (27 loc) · 690 Bytes
/
after.cpp
File metadata and controls
34 lines (27 loc) · 690 Bytes
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
#include <utility>
#include <string>
#include <type_traits>
template<typename Contained>
struct Optional
{
union { Contained data; };
bool initialized = false;
constexpr Optional &operator=(Contained &&data) {
this->data = std::move(data);
initialized = true;
return *this;
}
constexpr ~Optional() requires(!std::is_trivially_destructible_v<Contained>) {
if (initialized) {
data.~Contained();
}
}
constexpr ~Optional() = default;
};
int main()
{
Optional<int> obj;
obj = 5;
static_assert(std::is_trivially_destructible_v<Optional<int>>);
static_assert(!std::is_trivially_destructible_v<Optional<std::string>>);
}