1 | #include <boost/any.hpp>
|
---|
2 | #include <iostream>
|
---|
3 |
|
---|
4 | // Compile command
|
---|
5 | // clang++ -I [Path tho boost include directory] test.cpp
|
---|
6 |
|
---|
7 | /* OUTPUT
|
---|
8 | In file included from test.cpp:1:
|
---|
9 | /boost/1.61.0/include/boost/any.hpp:246:16: error: cannot initialize return object of type 'ExampleClass<int> *'
|
---|
10 | with an rvalue of type 'const int **'
|
---|
11 | return operand && operand->type() == boost::typeindex::type_id<ValueType>()
|
---|
12 | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
---|
13 | /boost/1.61.0/include/boost/any.hpp:263:27: note: in instantiation of function template specialization
|
---|
14 | 'boost::any_cast<ExampleClass<int> >' requested here
|
---|
15 | nonref * result = any_cast<nonref>(&operand);
|
---|
16 | ^
|
---|
17 | test.cpp:27:34: note: in instantiation of function template specialization 'boost::any_cast<ExampleClass<int> >' requested here
|
---|
18 | ExampleClass<int> returnedObj = boost::any_cast<ExampleClass<int> >(anyObj);
|
---|
19 | */
|
---|
20 |
|
---|
21 | template<typename T>
|
---|
22 | class ExampleClass
|
---|
23 | {
|
---|
24 | public:
|
---|
25 | ExampleClass(const T* p) : ptr(p)
|
---|
26 | {
|
---|
27 | }
|
---|
28 |
|
---|
29 | const T** operator &()
|
---|
30 | {
|
---|
31 | return &ptr;
|
---|
32 | }
|
---|
33 |
|
---|
34 | private:
|
---|
35 | const T* ptr;
|
---|
36 | };
|
---|
37 |
|
---|
38 |
|
---|
39 | int main(int argc, char *argv[])
|
---|
40 | {
|
---|
41 | ExampleClass<int> obj(new int(10));
|
---|
42 | boost::any anyObj (obj);
|
---|
43 |
|
---|
44 | ExampleClass<int> returnedObj = boost::any_cast<ExampleClass<int> >(anyObj);
|
---|
45 | std::cout << "Returned object is " << *(&returnedObj) << std::endl;
|
---|
46 |
|
---|
47 | return 0;
|
---|
48 | }
|
---|