C ++:混合:Boost :: Any + TypeID +指针:如果是指针,则克隆“通用”值
题
这是我想做的:
- 来自
boost::any
我想知道这是一种指针类型。 - 如果是指针,我必须克隆它
这样的事情:
boost::any value= new vector<string>();
if (typeid(value).IsPointerType())
{
boost::any newValue = Clone(value);
}
您认为这是可能的吗?
谢谢你的帮助
NB:我需要这个框架,该框架应该能够初始化默认值。
解决方案
您可以使用type_info接口:
#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;
}
返回
0
1
其他提示
您可以使用这样的东西(没有编译):
#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_; }
};
不隶属于 StackOverflow