Skip to main content
edited tags
Link
Mankarse
  • 40.9k
  • 12
  • 105
  • 146
Source Link
innectic
  • 275
  • 3
  • 12

Converting std::any into an unknown type

If I have a std::any of an std::string or an int, how could I cast this into the type that's contained?

std::any has type on it, however I can't use this type to cast.

Example:

#include <any> #include <iostream> #include <string> int main(void) { std::any test = "things"; std::any test2 = 123; // These don't work std::string the_value = (std::string) test; int the_value2 = (int) test2; std::cout << the_value << std::endl; std::cout << the_value2 << std::endl; }