-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathany.cpp
More file actions
87 lines (66 loc) · 2.35 KB
/
any.cpp
File metadata and controls
87 lines (66 loc) · 2.35 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
#include <iostream>
#include <iomanip>
#include <any>
#include <vector>
#include <string>
#include <unordered_map>
#include <typeindex>
#include <typeinfo>
#include <functional>
template <class T, class U>
const T to(const U& op);
template <class T, class U>
const T* to(const U* op);
template <class T, class F>
inline std::pair<const std::type_index,
std::function<void(std::any const&)>>
to_any_visitor(F const& f) {
return {
std::type_index(typeid(T)),
[f](std::any const& p) {
if constexpr (std::is_void_v<T>)
f();
else
f(std::any_cast<const T&>(p));
}
};
}
static std::unordered_map<std::type_index, std::function<void(std::any const&)>>
visitors {
to_any_visitor<void>([](void){std::cout << "void" << std::endl; }),
to_any_visitor<int>([](const int& p){ std::cout << "int:" << p << std::endl; }),
to_any_visitor<float>([](const float& f) { std::cout << "float:" << f << std::endl; }),
to_any_visitor<const char*>([](const char* ptr) { std::cout << std::quoted(ptr) << std::endl; })
};
template <class T>
inline void print(const T& param) { std::cout << param << std::endl; }
int main() {
using namespace std::literals;
std::vector<std::any> v;
v.push_back(std::make_any<int>(0));
v.push_back(2.2f);
v.push_back({"test"s});
for (const auto& p: v) {
if (p.type() == typeid(int)){ std::cout << std::any_cast<int>(p) << std::endl; }
else if(p.type() == typeid(float)){ std::cout << std::any_cast<float>(p) << std::endl; }
else if(p.type() == typeid(std::string)){ std::cout << std::any_cast<std::string>(p) << std::endl; }
else
throw std::bad_any_cast();
}
std::any i = std::make_any<int>(1);
const int* ptr = std::any_cast<int>(&i);
std::cout << *ptr << std::endl;
std::any a1 = 0;
std::any a2 = 0.1f;
std::any a3 = "test";
// types are very important
// T != T&
// T != const T&... so while casting being carefull in c++ is very important
print(std::any_cast<const int&>(a1));
print(std::any_cast<const float&>(a2));
print(std::any_cast<const char*&>(a3));
visitors[typeid(int)](a1);
visitors[typeid(float)](a2);
visitors[typeid(const char*)](a3);
return 0;
}