一般从boost :: variant< T>输入 [英] generically convert from boost::variant<T> to type

查看:119
本文介绍了一般从boost :: variant< T>输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 typedef boost :: variant< int,float,double,long,bool,std :: string,boost :: posix_time :: ptime> variant ,它用于在结构中存储不同类型的值。只有一个特定的类型将被存储在该结构中,但是我有一个这些结构的向量,我需要经过,并得到变体的实际类型。

I have a typedef boost::variant<int, float, double, long, bool, std::string, boost::posix_time::ptime> variant which I use to store different types of values in a struct. Only one of the specific type is ever going to be stored in that struct, however I have a vector of these structs which I need to go through and get the actual type out of the variant.

现在,当我需要做转换类型从这个变种,我这样做:

Now when I need to do the conversion the types out of this variant I do this:

variant second = mystruct.variant;
                                if (second.which() == 5) //string 
                    {
                        std::string val = boost::get<std::string>(second);
                        modvalue->AddNodeAttribute(key, val);
                    }
                    else if (second.which() == 0) //int
                    {
                        int val = boost::get<int>(second);
                        modvalue->AddNodeAttribute(key, val);
                    }
                    else if (second.which() == 2) //double
                    {
                        double val = boost::get<double>(second);
                        modvalue->AddNodeAttribute(key,val);
                    }
                    else if (second.which() == 1) //float
                    {
                        float val = boost::get<float>(second);
                        modvalue->AddNodeAttribute(key, val);
                    }
                    else if (second.which() == 3) // long
                    {
                        long val = boost::get<long>(second);
                        modvalue->AddNodeAttribute(key, val);
                    }
                    else if (second.which() == 4) // bool
                    {
                        bool val = boost::get<bool>(second);
                        modvalue->AddNodeAttribute(key, val);
                    }
                    else if (second.which() == 6) // posix::time
                    {
                        boost::posix_time::ptime ptm = boost::get<boost::posix_time::ptime>(second);
                        modvalue->AddNodeAttribute(key, ptm);
                    }



我想知道是否有一个更通用的方法,编写一个采用变量的类属函数和作为返回值的类型T.但是当我这样做的时候,我仍然需要写类似的一堆if语句来解释每种类型的T。

I wondered if there is a more generic way I can get around doing this by writing a generic function that takes the variant and a type T which is the return value. But when I do that I still have to write similar bunch of if statements to account for each type of T.

所以像 FromVariant< int> ;(var); ,但是我仍然必须为我的变体中的每个类型。

so something like FromVariant<int>(var);, but then I would still have to do it for each type in my variant.

所以看来,我的泛型解决方案不会减少我的代码,但增加它,这显然不是重点。我想知道是否有人有一个更优雅的解决方案来获得我的变种,有点泛型,我可以只调用一个函数给我的类型我想回来的各种类型。

So it seems that my generic solution doesn't reduce my code, but increase it, which obviously is not the point. I wondered if anyone has a more elegant solution to getting the various types out of my variant that is somewhat generic, where I can just call a function giving the type I want back?

推荐答案

实际上查看你的代码更多,这里是一个不同的选项 - 再次基于使用访问者..

Actually looking at your code some more, here is a different option - again based on using visitor..

struct add_node_visitor : boost::static_visitor<>
{
  add_node_visitor(<type of modvalue> & node, <type of key> & key) : _node(node), _key(key) {}

  template <typename _Item>
  void operator()(_Item const& item)
  {
    node->AddNodeAttribute(_key, item);
  }  

  <type of modvalue> & _node;
  <type of key> & _key;
}

使用:

boost::apply_visitor (add_node_visitor(modmodvalue, key), mystruct.variant);

只要您的 AddNodeAttribute 所有类型,以上应该工作...

As long as your AddNodeAttribute has overloads for all types, the above should work...

这篇关于一般从boost :: variant&lt; T&gt;输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆