[英]boost::mpl::vector - getting to a type's base-offset
在執行mpl::find<seq,type>
之后,是否可以獲得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<T>::ordinal
const硬編碼,我想盡可能避免這種情況。
重要說明 ,我也在向量中創建一個boost::variant
,我看到我可以通過執行運行時函數variant::which()
獲得序數。 但是,這需要我使用默認初始化值創建一個虛擬對象。 這非常糟糕。 如果您了解使用變體的其他方式,那么這也是我的問題的解決方案。
如果您正在尋找的是一種indexOf功能,我想Boost.MPL doc中有關find
的示例將起到作用:
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 );
它們是itterator類別中的一個元函數來做這個,它被稱為距離 。
ps,很快就回答我自己的問題而道歉。 我只是偶然發現了解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.