test_any.cxx
Go to the documentation of this file.
1 #include <boost/any.hpp>
2 #include <memory>
3 #include <iostream>
4 
5 using namespace std;
6 
7 struct Base {
8  virtual ~Base() {}
9  int b;
10 };
11 struct Foo : public Base {
12  virtual ~Foo() {}
13  int a;
14 };
15 
17 
18 int main()
19 {
20  boost::any aobj;
21  {
22  aobj = std::make_shared<Foo>();
23  }
24  auto base = boost::any_cast<std::shared_ptr<Foo> >(aobj);
25 
26  if (!base) {
27  cerr << "base is null\n";
28  }
29  else {
30  cerr << "base is not null\n";
31  }
32 
33  boost::any what = eos;
34  if (eos == boost::any_cast<DFPMeta>(what)) {
35  cerr << "Got eos from eos\n";
36  }
37 
38  DFPMeta dfpm;
39  try {
40  dfpm = boost::any_cast<DFPMeta>(aobj);
41  }
42  catch (boost::bad_any_cast &e) {
43  std::cerr << "aobj not a DFPMeta: " << e.what() << '\n';
44  dfpm = notdfp;
45  }
46  cerr << "DFPMeta = " << dfpm << "\n";
47 
48  return 0;
49 
50 }
int b
Definition: test_any.cxx:9
int a
Definition: test_any.cxx:13
STL namespace.
const double e
Definition: test_any.cxx:16
virtual ~Base()
Definition: test_any.cxx:8
int main()
Definition: test_any.cxx:18
DFPMeta
Definition: test_any.cxx:16
virtual ~Foo()
Definition: test_any.cxx:12