提高:: MPL :: vector的 - 让一个类型的基偏移 [英] boost::mpl::vector - getting to a type's base-offset

查看:117
本文介绍了提高:: MPL :: vector的 - 让一个类型的基偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时有可能得到在一个偏移MPL ::矢量执行后MPL ::找到<序列,类型> 就可以了?

Is it possible to get at the offset of a mpl::vector after performing a mpl::find<seq,type> on it ?

换句话说我想要做的编译时间equavalent:

Put differently I want to do the compile time equavalent of:

#include <vector>
#include <algorithm>
#include <iostream>

int main()
{
  typedef std::vector<int> v_type;
  v_type v_int(3);

  v_int[0] = 1;
  v_int[1] = 2;
  v_int[2] = 3;

  v_type::iterator it= std::find(  v_int.begin() ,v_int.end(),3);

  std::cout << it - v_int.begin() << std::endl;
}

如果做不到这一点,我的类型 MPL ::矢量有一个 type_trait&LT; T&GT; ::序常量硬 - codeD,我想避免这种可能的话

Failing this, my types in mpl::vector have a type_trait<T>::ordinal const hard-coded, I would like to avoid this if possible.

重要提示,我也创建一个的boost ::变种的载体,我看我可以通过执行获得的序运行时函数变种::其中()。然而,这需要我创建的默认初始化值的虚拟对象。这是相当uggly。如果你知道有变做了一些其他的方式,这将是一个解决我的问题也是如此。

Important Note, I am also creating a boost::variant from the vector, and I see I can get at the ordinal by performing a runtime function variant::which(). However, this requires I create a dummy object with default-initialized values. This is quite uggly. If you know some other way of doing it with variant, that would be a solution to my problem as well.

推荐答案

如果你正在寻找的是一种的indexOf特点,我想从Boost.MPL文档有关的例子查找将这样的伎俩:

If what you're looking for is a kind of indexOf feature, I guess the example from Boost.MPL doc concerning find will do the trick:

typedef vector<char,int,unsigned,long,unsigned long> types;
typedef find<types,unsigned>::type iter;

BOOST_MPL_ASSERT(( is_same< deref<iter>::type, unsigned > ));
BOOST_MPL_ASSERT_RELATION( iter::pos::value, ==, 2 );

这篇关于提高:: MPL :: vector的 - 让一个类型的基偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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