Template-defined number of template parameters (very meta)

Amr*_*ras 13 c++ generics templates

template<int Degree> using curve = // some definition
template<int MaxDegree> using curve_variant = std::variant<curve<1>, curve<2>, .. curve<MaxDegree>>;
Run Code Online (Sandbox Code Playgroud)

In the above example, the number of parameters passed to std::variant's template would change depending on curve_variant's parameters: For example, curve_variant<4> would resolve to std::variant<curve<1>, curve<2>, curve<3>, curve<4>>.

Since MaxDegree is known at compile time, this feels possible. But I also don't have a clue how to begin implementing this.

Jar*_*d42 15

有了std::integer_sequence助手,你可以这样做:

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;
Run Code Online (Sandbox Code Playgroud)