您可以随时自动创建包含原始集合及其容器包装对象的序列。
为此,我们需要一个一元的λ:
template <template <typename...> class Container>
struct MakeContainerOfT {
template <typename T>
struct impl {
using type = Container<T>;
};
};
这可以处理不同的地图(他们需要一个数值型)和数组每个集装箱(有一个非类型模板参数)。作为奖励,这里是工厂,使大小N
的数组:
template <std::size_t N>
struct ArrayOfT {
template <typename T>
struct impl {
using type = std::array<T, N>;
};
};
现在,我们需要对我们的一套适用本拉姆达(或任何其它一元拉姆达)为我们给出任何容器的设施。
template <typename Sequence, template <typename> class Transform>
struct TransformedSequenceBuilder {
using type = typename boost::mpl::reverse_fold<Sequence,
boost::mpl::set0<>,
boost::mpl::insert<boost::mpl::_1,
Transform<boost::mpl::_2>>>::type;
};
我们可以用“蓄压器”,将执行此转化lambda表达式的可变参数序列终于着手:
template <typename Sequence, template <typename> class... Transforms>
struct MakeFullSequence;
template <typename Sequence, template <typename> class Transform, template <typename> class... Tail>
struct MakeFullSequence<Sequence, Transform, Tail...> {
using type = typename boost::mpl::reverse_fold<typename MakeFullSequence<Sequence, Tail...>::type,
typename TransformedSequenceBuilder<Sequence, Transform>::type,
boost::mpl::insert<boost::mpl::_1, boost::mpl::_2>>::type;
};
template <typename Sequence>
struct MakeFullSequence<Sequence> {
typedef Sequence type;
};
的最后一步是定义一个别名为您感兴趣的容器:
template <typename Sequence>
using wrapped_set = typename MakeFullSequence<Sequence,
ContainerOfT<std::vector>::template impl,
ContainerOfT<std::set>::template impl//,
/* any transformation you fancy here */>::type;
为了测试这一点,我们可以执行一个平等的测试:
using le_set = boost::mpl::set<int, double, char>;
using le_vector_set = boost::mpl::set<std::vector<int>, std::vector<double>, std::vector<char>>;
using le_set_set = boost::mpl::set<std::set<int>, std::set<double>, std::set<char>>;
using le_transformed_set = wrapped_set<le_set>;
using le_manually_transformed_set = boost::mpl::joint_view<boost::mpl::joint_view<le_set, le_set_set>::type, le_vector_set>::type;
std::cout << boost::mpl::equal<le_transformed_set,
le_manually_transformed_set>::value;
用法则非常简单:用户只需要提供“原始”类型集合Set
每个需要它的时候,你分支的逻辑到wrapped_set<Set>
:
class Foo
{
typedef boost::mpl::set<bool, int, double> allowedTypes;
template<class T>
void some_templated_method()
{
BOOST_MPL_ASSERT((boost::mpl::has_key<wrapped_set<allowedTypes>, T>));
}
};
你可以找到演示说明我们做最后的原始集合,矢量包装集合和集合包装集合在这里:Live Demo
通过该设计,您还可以添加任何其他“重复风味”,你喜欢。引用(对应const
)对应的输入类型?只需通过std::add_lvalue_reference
(即std::add_const
)即可!
我只是让你知道我编辑我的答案有点让类型转换更通用。核心思想仍然是一致的,但是如果你需要,它可以让你更灵活:) – Rerito