lightxbulb
lightxbulb

Reputation: 1321

Parameter pack to parameter pack mapping

I want to remap a parameter pack to a different type parameter pack. More precisely I have a function:

template<typename ...type_pack, typename Function = void(type_pack&&...)>
        constexpr decltype(auto) zip(Function&& op, type_pack&&... pack)
        {
            static_for<0, N_lhs>([&](auto i)
                {
                    op(pack[i]...);
                });
            return;
        }

Basically I want to create a parameter pack of the results from applying the [] to the pack elements. Note that i here is an integral constant, and the static_for is compile time, you can assume that [] is constexpr. I do not have much control over op, so it expects a parameter pack and not a tuple.

Edit:

Seems like I was under the misunderstanding that op(pack[i]...) was causing the issue, when in fact this is a legal C++ construct (I thought it was illegal). So it seems like something was actually wrong with my static_for loop. My question was originally regarding op(pack[i]...) so I will keep it as is.

I prepared a more general example not using [] but a different arbitrary function just for a sanity check: https://godbolt.org/z/h8Hbbt

Is there a place in the standard where this pack expansion behaviour is mentioned - namely the fact that functions may be applied on top?

Upvotes: 1

Views: 515

Answers (2)

super
super

Reputation: 12928

A parameter pack can be expanded in terms of a pattern.

template <typename... T>
void test(T... t) {
    (std::cout << ... << static_cast<int>(t));
}

Here it is exanded as a folding expression, but it works in the same way in a regular pack expansion. The pattern here is static_cast<int>(t) and it will expand to

std::cout << static_cast<int>(t1) << static_cast<int>(t2) << ... << static_cast<int>(tN);

Upvotes: 1

max66
max66

Reputation: 66200

Basically I want to create a parameter pack of the results from applying the [] to the pack elements.

Do you mean something as follows?

template <typename ... type_pack,
          typename Function = void(decltype(std::declval<type_pack>()[0])...)>
constexpr decltype(auto) zip(Function&& op, type_pack&&... pack)
 {
   /* ... */
 }

Please, prepare a minimal but complete example (static_for, mainly) if you want a more tested answer.

Upvotes: 2

Related Questions