C++ : 混合 : boost::any +类型ID +指针:克隆“通用”;如果它是一个指针,则为值

发布于 2024-10-06 07:51:33 字数 347 浏览 1 评论 0原文

这是我想做的:

  • boost::any 我想知道它是一个指针类型。
  • 如果它是一个指针,我必须克隆它

类似这样的事情:

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

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

你认为这可能吗?

感谢您的帮助

注意:我需要一个应该能够初始化默认值的框架。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红焚 2024-10-13 07:51:33

你可以使用这样的东西(没有编译它):

#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 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_; }
};
鹤舞 2024-10-13 07:51:33

您可以使用 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文