CPP
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <any>
#include <iostream>
std::any getValue(int val) {
if (val < 0) return std::string("Negative value");
return val;
}
int main() {
std::any result = getValue(-5);
if (result.type() == typeid(std::string)) {
std::cout << std::any_cast<std::string>(result) << std::endl;
} else if (result.type() == typeid(int)) {
std::cout << std::any_cast<int>(result) << std::endl;
}
}
OUTPUT
Run