namespace boost {
class any {
public:
any();
any(const any&);
template<typename ValueType>
any(const ValueType&);
~any();
any& swap(any &);
any& operator=(const any&);
template<typename ValueType>
any& operator=(const ValueType&);
bool empty() const;
const std::type_info& type() const;
};
}#include <iostream>
#include <string>
#include <utility>
#include <vector>
#include <boost/any.hpp>
using namespace std;
struct A
{
void some_function() { cout << "A::some_function()" << endl; }
};
struct B
{
void some_function() { cout << "B::some_function()" << endl; }
};
struct C
{
void some_function() { cout << "C::some_function()" << endl; }
};
void print_any(boost::any& a)
{
if (A* pA=boost::any_cast<A>(&a))
{
pA->some_function();
}
else if (B* pB=boost::any_cast<B>(&a))
{
pB->some_function();
}
else if (C* pC=boost::any_cast<C>(&a))
{
pC->some_function();
}
else
{
try
{
cout << boost::any_cast<string>(a) << endl;
}
catch(boost::bad_any_cast &e)
{
cout << "Oops~" << e.what() << endl;
}
}
}
int main()
{
std::vector<boost::any> store_anything;
store_anything.push_back(A());
store_anything.push_back(B());
store_anything.push_back(C());
store_anything.push_back(string("This is fantastic! "));
store_anything.push_back(3);
store_anything.push_back(make_pair(true, 7.92));
for_each( store_anything.begin(),
store_anything.end(),
print_any);
}#include <iostream>
#include <string>
#include <boost/any.hpp>
using namespace std;
int main()
{
boost::any a1(100);
boost::any a2(std::string("200"));
boost::any a3;
cout << "a3 is " << (a3.empty() ? "empty" : "not empty" )<< endl;
a1.swap(a2);
try
{
string s=boost::any_cast<std::string>(a1);
cout << "a1 contains a string: " << s << endl;
}
catch(boost::bad_any_cast& e)
{
std::cout << "a1 doesn't contain a string! " << e.what() << endl;
}
if (int* p=boost::any_cast<int>(&a2))
{
cout << "a2 seems to have swapped contents with a1: " << *p << endl;
}
else
{
cout << "Nope, no int in a2" << endl;
}
if (typeid(int)==a2.type())
{
cout << "a2's type_info equals the type_info of int" << endl;
}
}#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <boost/any.hpp>
#include <boost/shared_ptr.hpp>
using namespace std;
class A
{
private:
string m_str;
public:
A(string str):m_str(str){}
virtual ~A() {cout << "A::~A()---" << m_str << endl; }
void not_virtual() {cout << "A::not_virtual()---" << m_str << endl;}
virtual void is_virtual () {cout << "A:: is_virtual ()---" << m_str<< endl;}
};
class B : public A
{
private:
string m_str;
public:
B(string str):A(str),m_str(str){}
~B(){cout << "B::~B()---" << m_str << endl;}
void not_virtual() {cout << "B::not_virtual()---" << m_str << endl;}
virtual void is_virtual () {cout << "B:: is_virtual ()---"<< m_str << endl;}
};
void foo(boost::any& a)
{
try
{
boost::shared_ptr<A> ptr = boost::any_cast<boost::shared_ptr<A> >(a);
ptr-> is_virtual ();
ptr->not_virtual();
return;
}
catch(boost::bad_any_cast& e) {}
try
{
boost::shared_ptr<B> ptr = boost::any_cast<boost::shared_ptr<B> >(a);
ptr-> is_virtual ();
ptr->not_virtual();
return;
}
catch(boost::bad_any_cast& e) {}
cout << "~~~~~~~other types~~~~~~~~" << endl;
}
int main()
{
boost::any a1(boost::shared_ptr<A>(new A("a1")));
boost::any a2(string("Just a string"));
{
boost::any b1(boost::shared_ptr<A>(new B("b1")));
boost::any b2(boost::shared_ptr<B>(new B("b2")));
vector<boost::any> vec;
vec.push_back(a1);
vec.push_back(a2);
vec.push_back(b1);
vec.push_back(b2);
for_each(vec.begin(),vec.end(),foo);
cout << endl;
}
}原文地址:http://blog.csdn.net/pngynghay/article/details/42774813