开发者

C++ : Mixing : boost::any + typeid + pointer : clone 'generic' value if it is a pointer

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.


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_; }
};


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜