-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
52 lines (43 loc) · 1.57 KB
/
example.cpp
File metadata and controls
52 lines (43 loc) · 1.57 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
#include <string>
#include <iostream>
#include <initializable.h>
typedef std::pair<std::string, std::string> InnerType;
template <Status T>
using StringPair = InitializableStruct<T, InnerType>;
typedef Initializable<InnerType> InitializableStringPair;
template <Status T>
using Age = InitializableStruct<T, unsigned>;
typedef Initializable<unsigned> InitializableAge;
struct Card {
std::string sender;
std::string receiver;
InitializableStringPair message = StringPair<UnInitialized>();
};
int main(){
Card birthday_card = Card{.sender="Mom", .receiver="Son"};
// Example showing that accessing an uninitialized value throws exception
try{
std::extract(birthday_card.message);
} catch (std::extract_uninitialized_value &e){
std::cout << e.what() << std::endl;
}
// Initialize the variable
std::initialize<InnerType>(birthday_card.message, std::make_pair("Harpy", "Birthday!"));
// Re-initialize, receive warning.
std::initialize<InnerType>(birthday_card.message, std::make_pair("Happy", "Birthday!"));
// Now the variable can be extracted
std::cout << std::extract(birthday_card.message).first
<< " "
<< std::extract(birthday_card.message).second
<< std::endl;
// If already initialized, can use assignment operator on contained value
std::get_initialized(birthday_card.message) = std::make_pair("HELLO", "WORLD");
std::cout << std::extract(birthday_card.message).first
<< " "
<< std::extract(birthday_card.message).second
<< std::endl;
if (!!birthday_card.message) {
std::cout << "message has already been added" << std::endl;
}
return 0;
}