A class template can have multiple parameters that all have defaults.
template
I came across this issue again and came up with a more general version of Sebastian Redl's solution.
//given an index to replace at, a type to replace with and a tuple to replace in
//return a tuple of the same type as given, with the type at ReplaceAt set to ReplaceWith
template
auto replace_type (std::index_sequence, std::tuple)
-> std::tuple...>;
//instantiates a template with the types held in a tuple
template class T, typename Tuple>
struct type_from_tuple;
template class T, typename... Ts>
struct type_from_tuple>
{
using type = T;
};
//replaces the type used in a template instantiation of In at index ReplateAt with the type ReplaceWith
template
struct with_n;
template class In, typename... InArgs>
struct with_n>
{
using tuple_type = decltype(replace_type
(std::index_sequence_for{}, std::tuple{}));
using type = typename type_from_tuple::type;
};
//convenience alias
template
using with_n_t = typename with_n::type;
Advantages:
options
and with_n_t<2,int,options<>>
are the same typeSome usage examples:
with_n_t<1, int, options<>> a; //options
with_n_t<2, int,
with_n_t<1, int, options<>>> b; //options
You could further generalise this to take variadic pairs of indices and types so that you don't need to nest with_n_t
.