1 #include <boost/variant.hpp> 2 #include <boost/any.hpp> 3 #include <vector> 4 #include <string> 5 #include <iostream> 6 7 std::vector<boost::any> vector; 8 9 struct output : 10 public boost::static_visitor<> 11 { 12 void operator()(double &d) const 13 { 14 vector.push_back(d); 15 std::cout << "double: " << d << std::endl; 16 } 17 18 void operator()(char &c) const 19 { 20 vector.push_back(c); 21 std::cout << "char: " << c << std::endl; 22 } 23 24 void operator()(std::string &s) const 25 { 26 vector.push_back(s); 27 std::cout << "string: " << s << std::endl; 28 } 29 }; 30 31 int main() 32 { 33 boost::variant<double, char, std::string> v; 34 v = 3.14; 35 boost::apply_visitor(output(), v); 36 v = 'A'; 37 boost::apply_visitor(output(), v); 38 v = "Hello, world!"; 39 boost::apply_visitor(output(), v); 40 }
http://zh.highscore.de/cpp/boost/datastructures.html