June 2025
Intermediate to advanced
1093 pages
33h 24m
English
If you are dissatisfied with having to specify the possible types when declaring variant, then perhaps any is for you. You can store a value of any type in it. And you can only retrieve that type. If you try with a different type, you will get an exception. However, you can assign any a new value and also a new type:
// https://godbolt.org/z/1eco9Wcr3#include <any>#include <iostream>#include <vector>#include <string>int main() { std::any a = 5; std::cout << std::any_cast<int>(a) << '\n'; a = 3.456; std::cout << std::any_cast<double>(a) << '\n'; using namespace std::literals; std::vector<std::any> data { 4, 8.976, "Geronimo"s }; std::cout << std::any_cast<double>( data[1] ) << '\n'; std::cout << data[1].type().
Read now
Unlock full access