How to fill tuple with result of calculation from template arguments parameter packs?

Viewed 45

I want to do some calculations on my template arguments and insert them into a tuple and return them. I cannot seem to get the syntax right though. Is it possible?

Godbolt link

#include <tuple>

template <typename TupleT, int ...values>
auto create_tuple() {
    // Fold expressions does not seem to work
    // return TupleT{((values + 1) , ...)};

    // This does not work either
    // return TupleT{(values + 1) , ...};

    // Expected result
    // return TupleT{1 + 1, 1 + 2}

}

int main() {
    auto t = create_tuple<std::tuple<int, int>, 1, 2>(); // Usage, cannot be changed
}

2 Answers

This is just an ordinary parameter pack that gets passed to a constructor.

return TupleT{(values + 1)...};

Live demo

you could also use std::make_tuple(...), I find it expressive and clean:

return std::make_tuple((values + 1)...);
Related