簡體   English   中英

使用 mpl::vector 定義 boost::variant 類型

[英]Using mpl::vector to define boost::variant types

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

#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的新手,所以也許我遺漏了一些東西 - 但找不到什么? 我過得好嗎?

提前致謝。

變體類型定義是正確的,問題是由於程序中的通用 function 將任意變體作為參數。 實際上, make_variant_over<mpl::vector<T0, T1, ...>>行為類似於variant<T0, T1, ...>但不是同一類型:它是一個variant<over_sequence<vector<T0, T1, ...>>> (所以 T0 對應於over_sequence<vector<T0, T1, ...>>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM