I just started learning C++17 fold expressions. I understand that it is possible to apply a fold expression to a tuple, like in the following example (inspired by replies to this question):
#include <iostream>
#include <tuple>
int main() {
std::tuple in{1, 2, 3, 4};
std::cout << "in = ";
std::apply([](auto&&... x) { ((std::cout << x << ' '), ...); }, in);
std::cout << std::endl;
std::multiplies<int> op;
auto out = std::apply([&](auto&& ...x) { return std::tuple{op(x, 3)...}; }, in);
std::cout << "out = ";
std::apply([](auto&&... x) { ((std::cout << x << ' '), ...); }, out);
std::cout << std::endl;
}
Output:
in = 1 2 3 4
out = 3 6 9 12
Is it possible to zip two tuples together using a similar approach? Referring to the example above, I would like to replace the constant 3 by another tuple, such as this hypothetical version of std::apply:
auto out = std::apply([&](auto&& ...x, auto&& ...y) { return std::tuple{op(x, y)...}; }, inX, inY);
In case fold expressions are not applicable to this purpose, is there an alternative method to achieve the same result in C++20 (other than template recursion and/oriSFINAE)?