CPP
cpp
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
#include <iostream>
#include <iomanip>
#include <any>
#include <type_traits>
using namespace std;
//typename cout
void typeName(any & x){
cout << "typeid: "
<< x.type().name() << endl;
}
//template function for any_visit
//(from Stack Overflow 52085547)
template <typename... T>
inline constexpr auto any_visit =
[](auto&& x, auto visit) -> enable_if_t<is_same_v<any, decay_t<decltype(x)>>> {
(
(x.type() == typeid(T) && (visit(any_cast<T>(forward<decltype(x)>(x))), true))
|| ...);
};
//overload << operator for any using generic function any_visit
ostream& operator<<(ostream& os, any const& a){
any_visit<int, float, double, char, char const*, bool>(a, [&os](auto v){ os << v; });
return os;
};
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run