C++ std::map holding ANY type of value

This is plain in C++ 17. Use std::map + std::any + std::any_cast: #include <map> #include <string> #include <any> int main() { std::map<std::string, std::any> notebook; std::string name{ “Pluto” }; int year = 2015; notebook[“PetName”] = name; notebook[“Born”] = year; std::string name2 = std::any_cast<std::string>(notebook[“PetName”]); // = “Pluto” int year2 = std::any_cast<int>(notebook[“Born”]); // = 2015 }

Type erasing type erasure, `any` questions?

This is a solution that uses C++14 and boost::any, as I don’t have a C++17 compiler. The syntax we end up with is: const auto print = make_any_method<void(std::ostream&)>([](auto&& p, std::ostream& t){ t << p << “\n”; }); super_any<decltype(print)> a = 7; (a->*print)(std::cout); which is almost optimal. With what I believe to be simple C++17 changes, … Read more