gpt4 book ai didi

c++ - 如何从 mpl::vector 实例化模板?

转载 作者:行者123 更新时间:2023-11-30 05:39:32 27 4
gpt4 key购买 nike

如何转换STL容器的vector?

我有:

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;

最佳答案

transform 接受一个需要是 Lambda Expression 的参数.最简单的 lambda 表达式是 Metafunction Class ,让我们开始吧。我们需要一个类,它有一个名为 apply 的嵌套类模板。产生一个名为 type 的类型.在我们的例子中,apply将采用本身就是 lambda 表达式的“参数”(在我们的示例中为 vector<_1><deque<_1> ),我们只需要 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;

我不明白为什么apply1在哪里工作 apply才不是。

关于c++ - 如何从 mpl::vector 实例化模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32319784/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com