I have a problem similar to
Generating a sequence of zeros at compile time
However, in my case I have no pack to expand. The situation is like this:
template<size_t N>
void foo()
{ bar(T{}, ..., static_cast<T>(1)); }
There are N - 1
T{}:s followed by a one.
It may happen that T
is not usable as a non-type template parameter. There are two differences compared to the linked post
- In the linked post, there is already a parameter pack to rely on
- In the linked post, it is assumed that all arguments are integers
I may use a solution that require C 20
CodePudding user response:
you generate the sequence yourself
#include <utility>
template<std::size_t N>
void foo(){
[&]<std::size_t...I>(std::index_sequence<I...>){
bar((I,T{}) ... , static_cast<T>(1));
}(std::make_index_sequence<N-1>());
}
CodePudding user response:
You can turn N
into std::index_sequence<0, .., N-1>
with std::make_index_sequence
, then you might expand your pack as you want:
template<size_t N>
void foo()
{
[]<std::size_t...Is>(std::index_sequence<Is...>){
bar(T{(static_cast<void>(Is), 0)}..., static_cast<T>(1));
} (std::make_index_sequence<N - 1>());
}
template lambda is C 20, if you cannot use it, you have to make a similar template helper function.