【问题标题】:Using mpl::vector to define boost::variant types使用 mpl::vector 定义 boost::variant 类型
【发布时间】:2011-07-21 09:29:44
【问题描述】:

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

提前致谢。

【问题讨论】:

  • 虽然是简化版,但问题中的代码可以在ideone上编译。这个document 说:由于几个编译器的标准一致性问题,make_variant_over 不是普遍可用的。在这些编译器上,库通过预处理器符号BOOST_VARIANT_NO_TYPE_SEQUENCE_SUPPORT 的定义表明它缺乏对语法的支持。 所以我建议检查BOOST_VARIANT_NO_TYPE_SEQUENCE_SUPPORT 的值。
  • 高级问题。由于变体的定义,这似乎不是问题,而是将变体作为输入boost::variant&lt;T0&gt; 的通用函数。此函数将T0 视为boost::detail::variant::over_sequence&lt;boost::mpl::vector&lt; ... types ...&gt; &gt;,暂时不明白为什么。

标签: c++ boost-mpl boost-variant


【解决方案1】:

变体类型定义是正确的,问题是由于程序中的泛型函数将任意变体作为参数。实际上,make_variant_over&lt;mpl::vector&lt;T0, T1, ...&gt;&gt; 的行为类似于 variant&lt;T0, T1, ...&gt;,但不是同一类型:它是 variant&lt;over_sequence&lt;vector&lt;T0, T1, ...&gt;&gt;&gt;(所以 T0 对应于 over_sequence&lt;vector&lt;T0, T1, ...&gt;&gt;。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多