here I have some class to test the boost::any_cast,usually I use intel compiler 10.0,but I got a message the application must be close.
but the ms vc 8.0 compiler works well.
My operator is windows xp,and I use vs2005 with inter compiler 10.0
here is my code:
-----------------------------------------------------------------------------------------------------------------------------------------
class CA
{
public:
 void tellme()
 {
  cout<<"I am ca"<<endl;
 }
};
 
class CB
{
public:
 void tellme()
 {
  cout<<"I am cb"<<endl;
 }
};
 
class CC
{
public:
 void tellme()
 {
  cout<<"I am cc"<<endl;
 }
};
 
class dotellme
{
public:
 void operator()(boost::any& ra)
 {
  if(CA* p = boost::any_cast<CA>(&ra))
  {
   p->tellme();
  }
  else if (CB* p = boost::any_cast<CB>(&ra))
  {
   p->tellme();
  }
  else if (CC* p = boost::any_cast<CC>(&ra))
  {
   p->tellme();
  }
  else if(string* p = boost::any_cast<string>(&ra))
  {
   cout<<*p<<endl;
  }
  else
  {
   int a;
   try
   {
    a = boost::any_cast<int>(ra);
    cout<<"it's int:"<<a<<endl;
   }
   catch (boost::bad_any_cast& e)
   {
    cout<<"OOPS"<<endl;
   }
  }
 }
};
 
//void tellme(boost::any& ra)
//{
// dotellme()(ra);
//}
int _tmain(int argc, _TCHAR* argv[])
{
 vector<boost::any> va;
 va.push_back(CA());
 va.push_back(CB());
 va.push_back(CC());
 va.push_back(string("haha,it a string"));
 va.push_back(dotellme());
 va.push_back(5);
 dotellme dt;
 for_each(va.begin(),va.end(),dotellme());
 va.clear();
 return 0;
}


Windows Live Writer,支持离线撰写博客内容,随时随地想写就写。 立即使用!