如何从参数包定义值类型的元组

它托尔曼克雷

我需要建立一个n类型的元组。这n种类型是n种其他类型的值类型。请考虑以下代码段:

#include <boost/hana.hpp>

namespace hana = boost::hana;

template<class... Types>
class CartesianProduct
{
public:
    CartesianProduct(Types... args) : sets(args...) {}

    hana::tuple<Types...> sets;
    hana::tuple<Types...::value_type> combination; // does not work obviously... but wo can this be done?
};

这样做的目的是这样的:我向此类传递了一个可能包含不同类型容器的参数包。该类将这些容器放入一个元组sets该类还具有一个字段combination该字段是与容器传递给该类一样多的元素的元组。但是元素的类型是不同容器的值类型。

然后,该类旨在延迟构建传递给它的容器的笛卡尔积,并将当前组合存储在中combination但是,我如何才能以可变的方式了解容器的价值类型呢?

讲故事的人-Unslander Monica

当然可以做到。您只需要适当地声明包扩展。

hane::tuple<typename Types::value_type...> combination; 

注意类型名称说明符的必需用法。经验法则是将包名称视为单一类型。应用相同的句法/语义约束,因为我们必须指定使用范围解析运算符访问类型。然后只需在最后扩展包装即可。

Live Example

#include <vector>
#include <map>
#include <tuple>

template<class... Types>
class CartesianProduct
{
public:
    CartesianProduct(Types... args) : sets(args...) {}

    std::tuple<Types...> sets;
    std::tuple<typename Types::value_type...> combination; 
};


int main() {
    std::vector<int> i;
    std::map<int, std::vector<int>> m;

    CartesianProduct<std::vector<int>, std::map<int, std::vector<int>>>
      c(i, m);

    return 0;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章