【问题标题】:How to instantiate templates from mpl::vector?如何从 mpl::vector 实例化模板?
【发布时间】:2023-03-17 14:45:01
【问题描述】:

如何转换 stl 容器的向量?

我有:

typedef boost::mpl::vector<std::vector<boost::mpl::_1>, std::deque<boost::mpl::_1> > Containers;

注意将其转换为:

typedef boost::mpl::vector<std::vector<int>, std::deque<int> > IntContainers;

怎么做,应该用什么代替“xxx”?

typedef boost::mpl::transform
<
    Containers,
    boost::mpl::**xxx**<boost::mpl::_1 , int>
>::type IntContainers;

【问题讨论】:

    标签: c++ templates boost template-meta-programming boost-mpl


    【解决方案1】:

    transform 接受一个需要为Lambda Expression 的参数。最简单的 lambda 表达式是 Metafunction Class,所以让我们开始吧。我们需要一个类,它有一个名为apply 的嵌套类模板,它产生一个名为type 的类型。在我们的例子中,apply 将采用一个“参数”,它本身就是一个 lambda 表达式(在我们的例子中是 vector&lt;_1&gt;&lt;deque&lt;_1&gt;),我们只需要 apply 它。那就是:

    template <typename Arg>
    struct call_with {
        template <typename T>
        struct apply {
            using type = typename boost::mpl::apply<T, Arg>::type;
        };  
    };
    
    using R = boost::mpl::transform<
        Containers,
        call_with<int>
    >::type;
    

    很可能通过巧妙地使用占位符将所有call_with 内联写入transform,但我发现这更容易理解。

    编辑显然这是对占位符的巧妙使用:

    using R = boost::mpl::transform<
        Containers,
        apply1<_1, int>
    >::type;
    

    我不明白为什么apply1apply 不工作的情况下工作。

    【讨论】:

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