std::any — type-erased container for any value.
1#include <any>2std::any a = 42;3std::any b = std::string("hello");45// Retrieve6if (a.type() == typeid(int)) {7 int val = std::any_cast<int>(a);8}910// Safe cast (returns nullptr on failure)11int* p = std::any_cast<int>(&a);
Use cases:
Alternatives:
std::variant for known types (faster)void* for C-style (unsafe)When to use: