#include <iostream>
#include <list>
#include <boost/any.hpp>
.
05.typedef std::list<boost::any> List_any;
.
07.//key part: Ability to store arbitrary types of objects
08.void fill_list (list_any& la)
09.{
La.push_back (1000);//Store constant
La.push_back (std::string ("This is an example"));//Store string object; note La.push_back ("Dyunze") error, due to being the wrong string array
.}
.
14.//the display based on type
15.void show_list (list_any& la)
16.{
List_any::iterator it;
Boost::any anyone;
.
For (it = La.begin (); It! = La.end (); it++)
. {
anyone = *it;
.
if (anyone.type () = = typeid (int))
std::cout<<boost::any_cast<int> (*it) <<std::endl;
. else if (anyone.type () = = typeID (std::string))
std::cout<<boost::any_cast<std::string> (*it). C_STR () <<std::endl;
.}
.}
.
31.int Main ()
32.{
list_any la;
fill_list (LA);
show_list (LA);
.
Panax Notoginseng return 0;
38.}
Output:
1000
This was an example
General methods of use of Boost::any