'call variadic template function on items in tuple of arrays
Let's say I have a function
template<typename retScalar, typename... scalars>
retScalar func_scalar(scalars... items);
declared somewhere.
I now want a "array" version of this function
template<typename retScalar, typename... scalars>
std::array<retScalar, LEN> func_vec(std::tuple<std::array<scalars, LEN>...> vecs) {
std::array<retScalar, LEN> res; // initialized somehow
for (int i = 0; i < LEN; ++i) {
res[i] = func_scalar(/* ?? */);
}
return res;
}
I searched a lot and don't see any correct way to do that.
Solution 1:[1]
Thanks to another anwser, I found this solution, which I find pretty cool and probably useful for other people
#include <cstdio>
#include <tuple>
template<typename T>
struct myvect {
T data[4];
};
template<typename... Ts>
struct helper {
template<std::size_t... I>
static auto inner(int index, std::tuple<myvect<Ts>...> vects, std::index_sequence<I...>)
{
return std::make_tuple(std::get<I>(vects).data[index]...);
}
};
template<typename... Ts>
auto elements(int index, std::tuple<myvect<Ts>...> vectors)
{
return helper<Ts...>::template inner(index, vectors, std::index_sequence_for<Ts...>{});
}
template<typename retS, typename... args>
struct vectorize {
template<retS(*funcptr)(args...)>
static myvect<retS> func_vect(std::tuple<myvect<args>...> v) {
myvect<retS> res;
for (int i = 0; i < 4; ++i) {
res.data[i] = std::apply(funcptr, elements(i, v));
}
return res;
}
};
int test(int a, int b) {
return a + b;
}
int main() {
myvect<int> a { 1, 2, 3, 4 };
myvect<int> b { 2, 4, 6, 8 };
auto added = vectorize<int, int, int>::func_vect<&test>(std::make_tuple(a, b));
for (int i = 0; i < 4; ++i) {
printf("%d\n", added.data[i]);
}
}
Solution 2:[2]
As of Next.js, this kind of problem occurs in development mode because of hot-module-replacement. But in production, it will only load once; therefore, don't worry about it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Regis Portalez |
| Solution 2 | Musaddik Rayat |
