Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify a default argument for a template parameter pack?

I have the following method

template <std::size_t N, std::size_t... Indices>
void doSomething()
{
    ...
};

It seems not possible to supply default values for Indices, e.g.

template <std::size_t N, std::size_t... Indices = std::make_index_sequence<N>>
void doSomething()
{
    ...
};

Compiler complains that

error: expected primary-expression before '>' token
template <std::size_t N, std::size_t... Indices = std::make_index_sequence<N>>
                                                                             ^~
error: template parameter pack 'Indices' cannot have a default argument
template <std::size_t N, std::size_t... Indices = std::make_index_sequence<N>>
                                    ^~~

Is there any way around this?

like image 563
zhanginou Avatar asked Nov 16 '25 23:11

zhanginou


1 Answers

There's already two excellent answers so far (including NathanOliver's comment). I think that having a couple of overloads that act as wrappers is really the simplest solution here, but just for completeness, let's provide your desired functionality in one function:

Often it's best to take as much of the template logic out of the parameter list as possible (separating inputs from logic):

template <std::size_t N, std::size_t... Indices>
void doSomething()
{
    using Ints
        = std::conditional_t<
            /* if */ (sizeof...(Indices) > 0),
                std::index_sequence<Indices...>,
            /* else */
                std::make_index_sequence<N>>;

    // ...
};

demo

like image 158
Elliott Avatar answered Nov 18 '25 13:11

Elliott