-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstd_any_example.cc
More file actions
129 lines (106 loc) · 3.28 KB
/
std_any_example.cc
File metadata and controls
129 lines (106 loc) · 3.28 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,
C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, COBOL, HTML, CSS, JS
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <any>
#include <map>
#include <iostream>
#if 0
int main(){
void* _value;
// set the value as int
int a =30;
_value=&a;
// we can read it as int
std::cout << "int: "<<*((int*)_value)<<'\n';
// set the value as bool
bool b =true;
_value=&b;
// read the value as bool
std::cout <<"bool: " <<*((bool*)_value)<<'\n';
return 0;
}
#else
struct MyType
{
int a, b;
MyType(int x, int y) : a(x), b(y) { }
void Print() { std::cout << a << ", " << b << "\n"; }
};
int main()
{
/* Creation */
// default initialization:
std::any b;
std::cout << "Default initialization: "<< b.has_value()<<'\n';
// initialization with an object
std::any a(12);
std::cout << "Initialization with an object: "<< a.has_value()<<'\n';
// make_any
std::any a6 = std::make_any<std::string>("Hello World");
std::cout << "Initialization of an object using make_any: "<< a6.has_value()<<'\n';
/* Changing the Value */
// set any value:
a = std::string("Hello!");
a = 16;
// we can read it as int
std::cout << std::any_cast<int>(a) << '\n';
// using emplace public function
a.emplace<float>(100.5f);
// we can read it as float
std::cout << std::any_cast<float>(a) << '\n';
// but not as string:
try
{
std::cout << std::any_cast<std::string>(a) << '\n';
}
catch(const std::bad_any_cast& e)
{
std::cout << e.what() << '\n';
}
// reset and check if it contains any value:
a.reset();
if (!a.has_value())
{
std::cout << "a is empty!" << "\n";
}
// you can use it in a container:
std::map<std::string, std::any> m;
m["integer"] = 10;
m["string"] = std::string("Hello World");
m["float"] = 1.0f;
for (auto &[key, val] : m)
{
if (val.type() == typeid(int))
std::cout << "int: " << std::any_cast<int>(val) << "\n";
else if (val.type() == typeid(std::string))
std::cout << "string: " << std::any_cast<std::string>(val) << "\n";
else if (val.type() == typeid(float))
std::cout << "float: " << std::any_cast<float>(val) << "\n";
}
std::any var = std::make_any<MyType>(10, 10);
try
{
std::any_cast<MyType&>(var).Print(); // to return a copy of the value, and throw std::bad_any_cast when it fails
std::any_cast<MyType&>(var).a = 11; // read/write return a reference (also writable), and throw std::bad_any_cast when it fails
std::any_cast<MyType&>(var).Print();
std::any_cast<int>(var); // throw!
}
catch(const std::bad_any_cast& e)
{
std::cout << e.what() << '\n';
}
// to return a pointer to the value (const or not) or nullptr on failure
int* p = std::any_cast<int>(&var);
std::cout << (p ? "contains int... \n" : "doesn't contain an int...\n");
MyType* pt = std::any_cast<MyType>(&var);
if (pt)
{
pt->a = 12;
std::any_cast<MyType&>(var).Print();
}
return 0;
}
#endif