-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.cc
40 lines (33 loc) · 1.16 KB
/
example.cc
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
#include "any_ptr.hh"
#include <vector>
#include <iostream>
int main() {
std::vector<double>* DoubleVecPtr = new std::vector<double>;
DoubleVecPtr->push_back(1.1);
DoubleVecPtr->push_back(2.2);
DoubleVecPtr->push_back(3.3);
std::vector<any_ptr> ptrs;
ptrs.emplace_back(DoubleVecPtr);
ptrs.emplace_back(new std::vector<int>({4,5,6}));
ptrs.push_back(make_any<std::vector<double>>({7.7,8.8,9.9}));
for (auto& ptr : ptrs)
{
if (auto runtime_casted_ptr = std::shared_ptr<std::vector<double>>(ptr))
{
std::cout << "Runtime cast to std::vector<double> successful!\n";
for (auto const& dbl : *runtime_casted_ptr)
{
std::cout << dbl << std::endl;
}
}
else if (auto runtime_casted_ptr = std::shared_ptr<std::vector<int>>(ptr))
{
std::cout << "Runtime cast to std::vector<int> successful!\n";
for (auto const& integer : *runtime_casted_ptr)
{
std::cout << integer << std::endl;
}
} else { std::cout << "Unsuccessful runtime cast from any_ptr\n"; }
}
return 0;
}