You can use boost::any . For instance:
#include <vector> #include <boost/any.hpp> #include <iostream> struct my_class { my_class(int i) : x{i} { } int x; }; int main() { std::vector<boost::any> v; v.push_back(42); v.push_back(std::string{"Hello!"}); v.push_back(my_class{1729}); my_class obj = boost::any_cast<my_class>(v[2]); std::cout << obj.x; }
If you want to limit the set of allowed types to a specific range, instead of boost::variant :
#include <vector> #include <boost/variant.hpp> #include <iostream> struct my_class { my_class(int i) : x{i} { } int x; }; int main() { typedef boost::variant<int, std::string, my_class> my_variant; std::vector<my_variant> v; v.push_back(42); v.push_back("Hello!"); v.push_back(my_class{1729}); my_class obj = boost::get<my_class>(v[2]); std::cout << obj.x; }
boost::variant also supports visiting. You can define a visitor who can handle all possible types in a variant:
struct my_visitor : boost::static_visitor<void> { void operator () (int i) { std::cout << "Look, I got an int! " << i << std::endl; } void operator () (std::string const& s) { std::cout << "Look, I got an string! " << s << std::endl; } void operator () (my_class const& obj) { std::cout << "Look, I got a UDT! And inside it a " << obj.x << std::endl; } };
And then call it as shown below:
int main() { typedef boost::variant<int, std::string, my_class> my_variant; std::vector<my_variant> v; v.push_back(42); v.push_back("Hello!"); v.push_back(my_class{1729}); my_visitor mv; for (auto const& e : v) { e.apply_visitor(mv); } }
Here is a living example . The best part about boost::variant is that it will run a compile-time check to make sure your visitor can handle all the types that this option can contain.
source share