使用mpl::vector定义boost::变体类型

Using mpl::vector to define boost::variant types

本文关键字:boost 类型 定义 mpl vector 使用      更新时间:2023-10-16

我使用库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所以也许我错过了一些东西-但找不到什么!我做得好吗?

变量类型定义是正确的,问题是由于程序中的泛型函数将任意变量作为参数。事实上,make_variant_over<mpl::vector<T0, T1, ...>>的行为类似于variant<T0, T1, ...>,但不是相同的类型:它是variant<over_sequence<vector<T0, T1, ...>>>(因此T0对应于over_sequence<vector<T0, T1, ...>>)。

相关文章: