2011-07-21 42 views
4

我正在使用庫boost::variant來存儲大量的類型。隨着類型數量的增長,我很快會達到20種類型的限制。在文檔中,似乎可以使用mpl::vector來定義變體,該變體允許超過20種類型(如果我是正確的,則最多爲50個)。我試圖替換我的變體定義是這樣的:使用mpl :: vector來定義boost :: variant類型

#include <boost/variant.hpp> 
#include <boost/mpl/vector.hpp> 

typedef boost::mpl::vector< 
    float, 
    math::float2, 
    math::float3, 
    relative_point<1>, 
    relative_point<2>, 
    relative_point<3>, 
    std::string, 
    color, 
    group, 
    dictionnary, 
    reference, 
    line, 
    strip, 
    text, 
    font 
> variant_mpl_vec; 

typedef boost::make_variant_over<variant_mpl_vec>::type data_type; 

// This is the old definition 
/*typedef boost::variant< 
    float, 
    math::float2, 
    math::float3, 
    relative_point<1>, 
    relative_point<2>, 
    relative_point<3>, 
    std::string, 
    color, 
    group, 
    dictionnary, 
    reference, 
    line, 
    strip, 
    text, 
    font 
> data_type;*/ 

我直接把我的代碼。大多數類型都是包含非常少數據的結構。

編譯時,我得到了一個奇怪:

error: no matching function for call to ‘boost::detail::variant::make_initializer_node::apply<boost::mpl::pair< ... and lots more ... 

上變型 - 定義是工作的罰款,所以我很驚訝我的更換不起作用。我是mpl的新手,所以也許我錯過了一些東西 - 但找不到什麼!我做得好嗎?

在此先感謝。

+0

雖然與簡化版本, 在問題的代碼可以對 編譯[ideone](HTTP:/ /ideone.com/Sgx02)。 這個[document](http://www.boost.org/doc/libs/1_47_0/doc/html/variant/tutorial.html#variant.tutorial.over-sequence) 說: 由於在幾個編譯器中的標準一致性問題,'make_variant_over' 並不是普遍可用的。在這些編譯器中,庫指示它的 缺少通過預處理符號定義的語法支持 'BOOS T_VARIANT_NO_TYPE_SEQUENCE_SUPPORT'._ 所以我建議檢查'BOOST_VARIANT_NO_TYPE_SEQUENCE_SUPPORT'的值。 –

+0

問題中的高級功能。看起來這不是一個問題,因爲變體的定義,而是一個泛型函數,它將一個變體作爲輸入'boost :: variant '。這個函數將'T0'視爲'boost :: detail :: variant :: over_sequence >',不明白爲什麼暫時。 – neodelphi

回答

1

變體類型定義是正確的,問題是由於程序中的泛型函數以任意變體作爲參數。事實上,make_variant_over<mpl::vector<T0, T1, ...>>行爲就像variant<T0, T1, ...>但是不一樣的類型的:它是一個variant<over_sequence<vector<T0, T1, ...>>>(所以T0對應於over_sequence<vector<T0, T1, ...>>

相關問題