Ticket #12615: test.cpp

File test.cpp, 1.3 KB (added by vishalshetye@…, 6 years ago)

sample code

Line 
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
8In 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> *'
10with an rvalue of type 'const int **'
11return 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
15nonref * result = any_cast<nonref>(&operand);
16^
17test.cpp:27:34: note: in instantiation of function template specialization 'boost::any_cast<ExampleClass<int> >' requested here
18ExampleClass<int> returnedObj = boost::any_cast<ExampleClass<int> >(anyObj);
19*/
20
21template<typename T>
22class ExampleClass
23{
24public:
25 ExampleClass(const T* p) : ptr(p)
26 {
27 }
28
29 const T** operator &()
30 {
31 return &ptr;
32 }
33
34private:
35 const T* ptr;
36};
37
38
39int 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}