Search code examples
c++parameter-pack

Generate a sequence of null values follwed by one at compiletime


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

  1. In the linked post, there is already a parameter pack to rely on
  2. In the linked post, it is assumed that all arguments are integers

I may use a solution that require C++20


Solution

  • 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>());
    }