4

是否有可能在得到執行就可以了mpl::find<seq,type>mpl::vector的偏移?的boost :: MPL ::向量 - 讓爲類型的基本抵消

換句話說我想要做的編譯時間equavalent:

#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硬編碼的,我想如果可能避免這種情況。

重要提示,我也創建從向量boost::variant,我看我可以通過執行運行時函數variant::which()在序得到。但是,這需要我使用默認初始化值創建一個虛擬對象。這很不好。如果你知道其他方式使用變體,這也是我的問題的解決方案。

回答

3

如果你正在尋找的是一種的indexOf功能,我想從Boost.MPL商務部關於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); 
+0

AAH的'iter'有一個'pos'元素?我找不到它的itterator概念文檔中http://www.boost.org/doc/libs/1_46_1/libs/mpl/doc/refmanual/iterators-concepts.html – 2011-05-18 11:50:57

+0

雖然我無法找到一個POS' '成員,當我查看迭代器概念文檔時,它顯示在'mpl :: find'文檔的示例中(與您所顯示的代碼片段相同)。 – 2011-05-18 11:58:17

+0

@Hassan賽義德:你可能有興趣在這兩個這個問題,其回答有關[無證ITER :: POS(http://stackoverflow.com/questions/5666394/is-mpl-pos-an-undocumented-metafunction) – 2011-05-18 12:04:53

1

他們是在itterator類別元函數來做到這本,它被稱爲distance

P.S.,這麼快回答我的問題道歉。我偶然發現了這個解決方案。