How to "duplicate" template parameter pack expansion?


Consider this code:

template < size_t... Indices >
void something(std::index_sequence<Indices...>)
  // how to obtain the following call where N is sizeof...(Indices)?
  // foo(f(0),g(0),f(1),g(1),...,f(N-1),g(N-1));

The key to answering a question like:

// how to obtain the following call where N is sizeof...(Indices)?
// foo(f(0),g(0),f(1),g(1),...,f(N-1),g(N-1));

is to formulate how to actually generate such a pack expansion. We have 2N arguments, which would like:

| idx | expr |
|  0  | f(0) |
|  1  | g(0) |
|  2  | f(1) |
|  3  | g(1) |
|   ....

In C++17, we can write such an expression with if constexpr:

template <size_t I>
auto expr() {
    if constexpr (I % 2 == 0) { 
        // even indices are fs
        return f(I / 2);
    } else {
        // odds are gs
        return g(I / 2);

Which can even be a lambda that takes an integral_constant as an argument. So we really just need to turn our N arguments into 2N arguments, which is just a matter of adding more indices:

template <auto V>
using constant = std::integral_constant<decltype(V), V>;

template < size_t... Indices >
void something(std::index_sequence<Indices...>) {
    auto expr = [](auto I) {
        if constexpr (I % 2 == 0) {
            return f(I / 2);
        } else {
            return g(I / 2);

    return foo(
        expr(constant<Indices>{})...,                     // 0, ..., N-1
        expr(constant<Indices + sizeof...(Indices)>{})... // N, ..., 2N-1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at


Login to comment


Variadic template pack expansion

Pack expansion for alias template

when template parameter of a template template-parameter is pack expansion, gcc fails, clang succeeds

Clang fails to compile parameter pack expansion using template metaprogramming

How to fill array with contents of a template parameter pack?

Order of parameter pack expansion

Simultaneous parameter pack expansion error for unused template type definition

Aliasing a template parameter pack

Having trouble with parameter pack expansion

Nested parameter pack expansion

How to accumulate template parameter pack?

Class template parameter pack expansion for constructors

template Parameter pack expansion on function call with array of data

How do I get access to template parameters of a template pack parameter

How to use and access a template parameter pack of parameter packs

Is a parameter pack a template parameter?

g++ and clang++ are both bugged with template function parameter pack expansion?

How exactly is expansion of a parameter pack evaluated with std::forward?

Nested parameter pack expansion fails

Template Meta Programming: How to combine parameter packs to new parameter pack

How to use enable if with template arguments and parameter pack?

aliassing a template parameter pack

double template parameter pack

Variadic template indexed pack expansion

a variadic template as a template parameter without pack expansion

Parameter pack expansion questions

parameter pack expansion not working in lambda

C++ template variable and parameter pack expansion

Template pack expansion with functions