构造包含变体类型索引中第n类型值的boost变体? [英] Construct a boost variant containing a value of the nth-type in the variant type index?

查看:98
本文介绍了构造包含变体类型索引中第n类型值的boost变体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想构造 boost :: variant 包含默认构造的值,用类型索引指定 - 不在类型索引上写自己的switch语句。 p>

我认为这个必须以某种方式使用MPL吗?



,索引不是编译时常量表达式。



用例是我需要构造一个变体,其中一个包含正确的值,但在这一点上我只知道类型索引。

解决方案

您需要使用 variant :: types typedef。这给了一个MPL兼容的序列,然后我们可以使用 mpl :: at 和模板来做我们的投标。这就是诀窍:

  #include< string> 
#include< boost / variant.hpp>
#include< boost / mpl / at.hpp>
#include& lt; boost / mpl / int.hpp>

template< typename U,typename V>
void construct_in(V& v){
v = U();
// modern
// v = U {};
}

int main()
{
typedef boost :: variant< int,std :: string>变体;
typedef boost :: mpl :: at< variant :: types,boost :: mpl :: int_< 1>> :: type pos;
variant v;
//使用类型deduction
construct_in< pos>(v);
//不抛出,工作
std :: string& s = boost :: get< std :: string>(v);
return 0;
}

这里是运行时变式:

  #include< string> 
#include< vector>
#include< functional>

#include< boost / variant.hpp>
#include< boost / mpl / at.hpp>
#include< boost / mpl / int.hpp>
#include< boost / mpl / for_each.hpp>

typedef boost :: variant< int,std :: string>变体;
typedef variant :: types types;
typedef std :: vector< std :: function< void(variant&)> > fvec;

template< typename U,typename V>
void construct_in(V& v){
v = U {};
}

struct build_and_add {
fvec * funcs;
template< typename T>
void operator()(T){
funcs-> push_back(& construct_in< T,variant>);
}
};


int main()
{

variant v;
std :: vector< std :: function< void(variant&)> >功能

//不能使用lambda,需要是多态的
build_and_add f = {& funcs};
boost :: mpl :: for_each< types>(f);

//这是运行时!
int i = 1;

funcs [i](v);
//不抛出,工作
std :: string& s = boost :: get< std :: string>(v);
return 0;
}

这有点神秘,需要一些调整
参数真正的通用,但它做你想要的。有人需要弄清楚这是否会导致大量的
代码崩溃。


I want to construct boost::variants containing default-constructed values, specified with a type index - without writing my own switch statement over the type index.

I figure this must be possible, somehow, with MPL?

To clarify though, the index is not a compile-time constant expression.

The use case is that I need to construct a variant which will later be replaced with one containing the correct value, but at this point I only know the type index. Think of it as a lazy deserialisation problem.

解决方案

You need to use the variant::types typedef. This gives you an MPL compatible sequence which we can then use with mpl::at and a template to do our bidding. This does the trick:

#include <string>
#include <boost/variant.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/int.hpp>

template<typename U, typename V>
void construct_in(V& v) {
  v = U();
  // modern
  // v = U{};
}

int main()
{
  typedef boost::variant<int, std::string> variant;
  typedef boost::mpl::at<variant::types, boost::mpl::int_<1>>::type pos;
  variant v;
  // use type deduction
  construct_in<pos>(v);
  // does not throw, does work
  std::string& s =boost::get<std::string>(v);
  return 0;
}

Here goes the runtime-variant:

#include <string>
#include <vector>
#include <functional>

#include <boost/variant.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/for_each.hpp>

typedef boost::variant<int, std::string> variant;
typedef variant::types types;
typedef std::vector< std::function<void(variant&)> > fvec;

template<typename U, typename V>
void construct_in(V& v) {
  v = U{};
}

struct build_and_add {
  fvec* funcs;
  template<typename T>
  void operator()(T) {
    funcs->push_back(&construct_in<T, variant>);
  }
};


int main()
{

  variant v;
  std::vector< std::function<void(variant&)> > funcs;

  // cannot use a lambda, would need to be polymorphic
  build_and_add f = {&funcs};
  boost::mpl::for_each<types>(f);

  // this is runtime!
  int i = 1;

  funcs[i](v);
  // does not throw, does work
  std::string& s =boost::get<std::string>(v);
  return 0;
}

It's a little arcane and will need some tweaking with variadic arguments to be truly generic, but it does what you want. Someone else needs to figure out if this results in significant code blow-up.

这篇关于构造包含变体类型索引中第n类型值的boost变体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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