Question

Here is what I would like to do:

  • From a boost::any I would like to know it is a pointer type.
  • If it is a pointer, I have to clone it

Something like this :

boost::any value= new vector<string>();

if (typeid(value).IsPointerType())
{
  boost::any newValue = Clone(value);
}

Do you think that it is possible ?

Thanks for your help

NB: I need this for a framework that should be able to initialize Default value.

Was it helpful?

Solution

You can use the type_info interface:

#include <boost/any.hpp>
#include <iostream>
#include <typeinfo>

using namespace std;

int main()
{
    boost::any intVal = 5;

    int* a = new int(6);
    boost::any ptrVal = a;

    cout << intVal.type().__is_pointer_p() <<endl;
    cout << ptrVal.type().__is_pointer_p() << endl; 

    return 0;
}

Returns

0
1

OTHER TIPS

You could use something like this (didn't compile it):

#include <boost/type_traits.hpp>

class any_p: public boost::any {
    const bool is_ptr_;
public:
    template<class T>
    any_p(T obj): boost::any(obj), is_ptr_(is_pointer<T>::value_type) {}
    const bool is_ptr() const { return is_ptr_; }
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top