template<int Degree> using curve = // some definition
template<int MaxDegree> using curve_variant = std::variant<curve<1>, curve<2>, .. curve<MaxDegree>>;
在上面的例子中,传递给 std::variant 模板的参数数量会根据 curve_variant 的参数而变化:例如,
curve_variant<4>
将解析为 std::variant<curve<1>, curve<2>, curve<3>, curve<4>>
.由于 MaxDegree 在编译时是已知的,这感觉是可能的。但我也不知道如何开始实现这一点。
最佳答案
与 std::integer_sequence
helper ,你可能会这样做:
template <typename Seq> struct curve_variant_impl;
template <int ... Is>
struct curve_variant_impl<std::integer_sequence<int, Is...>>
{
using type = std::variant<curve<1 + Is>...>;
};
template <int MaxDegree>
using curve_variant = typename curve_variant_impl<std::make_integer_sequence<int, MaxDegree>>::type;
关于c++ - 模板定义的模板参数数量(非常元),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63633004/