简体   繁体   English

使用 mpl::vector 定义 boost::variant 类型

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

I'm using the library boost::variant to store a large number of types.我正在使用库boost::variant来存储大量类型。 As the number of type is growing, I will soon reach the limit of 20 types.随着类型的数量越来越多,我很快就会达到 20 种的限制。 In the documentation it seems possible to define the variant using a mpl::vector , which allows more than 20 types (up to 50 if I'm correct).在文档中,似乎可以使用mpl::vector定义变体,它允许超过 20 种类型(如果我是正确的,最多 50 种)。 I tried to replace my variant definition like this:我试图像这样替换我的变体定义:

#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;*/

I'm putting directly my code.我直接把我的代码。 Most of types are structures containing very few data.大多数类型是包含很少数据的结构。

When compiling, I got a strange:编译时,我得到一个奇怪的:

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

Previous variant definition was working fine, so I'm surprised my replacement doesn't work.以前的变体定义工作正常,所以我很惊讶我的替换不起作用。 I'm new to mpl so maybe I'm missing something - but can't find what?我是mpl的新手,所以也许我遗漏了一些东西 - 但找不到什么? Am'I doing well ?我过得好吗?

Thanks in advance.提前致谢。

Variant type definition is correct, the problem was due to a generic function in the program taking an arbitrary variant as parameter.变体类型定义是正确的,问题是由于程序中的通用 function 将任意变体作为参数。 Indeed, make_variant_over<mpl::vector<T0, T1, ...>> behaves like variant<T0, T1, ...> but is not the same type: it is a variant<over_sequence<vector<T0, T1, ...>>> (so T0 corresponds to over_sequence<vector<T0, T1, ...>> .实际上, 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