Assume a sequence of variadic parameter types is to be generated according to a index sequence, i.e, given a tuple
using Tuple = std::tuple<int, float, bool>;
and a function signature as following:
template <std::size_t ...Is>
void func(std::index_sequence<Is...>, std::tuple_element_t<Is, Tuple>...) {}
a invocation like func(std::make_index_sequence<2>(), 2, 1.0)
would compile without problem.
However, if we were to compute the size of std::index_sequence
according to another type, and therefore place std::index_sequence
at the end of parameter list with a default argument, just like this:
template <typename T, std::size_t ...Is>
void func(T, std::tuple_element_t<Is, Tuple>..., std::index_sequence<Is...> = std::make_index_sequence<some_computation_v<T>>()) {}
The compilation of g++ and clang++ would succeed if and only if the size of std::index_sequence
is 0, that is, for following signature:
template <std::size_t... Is>
void func(std::tuple_element_t<Is, Tuple>..., std::index_sequence<Is...> = std::make_index_sequence<2>()) {}
func(2, 1.0)
or func(2, 1.0, std::make_index_sequence<2>())
would result in error:
main.cpp:34:6: note: template argument deduction/substitution failed:
main.cpp:43:16: note: mismatched types ‘std::integer_sequence<long unsigned int, _Idx ...>’ and ‘int’
func(2, 1.0);
This prevents me from restricting the types of variadic template with a key type T, by mapping T to a tuple of parameter types and expand it using std::index_sequence
.
Is there any way to resolve this error or to meet the needs mentioned above?
question from:
https://stackoverflow.com/questions/65870951/failed-resolution-of-variadic-parameter-types-with-post-positioned-stdindex-se