boost::mpl::vector - 获取类型的基址偏移量
在执行 mpl::find
后是否可以获取 mpl::vector
的偏移量?
换句话说,我想做的编译时等效:
#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::vector 中的类型有一个 type_trait
const 硬编码,如果可能的话我想避免这种情况。
重要说明,我还从向量创建了 boost::variant
,并且我发现我可以通过执行运行时函数 variant:: 来获取序数其中()。但是,这需要我创建一个具有默认初始化值的虚拟对象。这是相当难看的。如果您知道使用变体执行此操作的其他方法,那也可以解决我的问题。
Is it possible to get at the offset of a mpl::vector
after performing a mpl::find<seq,type>
on it ?
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;
}
Failing this, my types in mpl::vector
have a type_trait<T>::ordinal
const hard-coded, I would like to avoid this if possible.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您要寻找的是一种 indexOf 功能,我想 Boost.MPL 文档中有关
find
的示例可以解决问题: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:它们是迭代器类别中的一个元函数来执行此操作,它被称为 距离。
ps,很抱歉这么快回答我自己的问题。我只是偶然发现了解决方案。
Their is a metafunction in the itterator category to do just this, it is called distance.
p.s., apologies for answering my own question so quickly. I just stumbled on the solution.