Saturday, December 19, 2009

A simple illustration on Boost::any

#include <iostream>
#include <vector>

#include <boost/any.hpp>

using namespace
std;
using namespace
boost;

int
main(){

vector<any> v1, v2;

v1.push_back(5);

v1.push_back('a');

v2.push_back(10);

v2.push_back(string("Amma"));

copy(v2.begin(),v2.end(),back_inserter(v1));

for
(vector<any>::size_type i = 0; i < v1.size(); i++){

if
(char * pc = any_cast<char>(&v1[i])) cout << *pc << endl;

else if
(int * pi = any_cast<int>(&v1[i])) cout << *pi << endl;

else if
(string * ps = any_cast<string>(&v1[i])) cout << *ps << endl;
}
}


No comments: