Reference:
std::apply
std::index_sequence
<utility>
index_sequence is quite handy with argument deduction.
#include <utility>
#include <array>
template <typename... Args>
struct CustomType { };
template <typename... Args>
CustomType<Args...> method(const Args&... args) { return {}; }
template <typename T, std::size_t N, typename VariadicFunc, std::size_t... I>
auto methodApply(
const std::array<T, N>& arr, VariadicFunc func, std::index_sequence<I...>) {
return func(arr[I]...);
}
template <typename T,
std::size_t N,
typename VariadicFunc,
typename Indices = std::make_index_sequence<N>>
auto methodArr(const std::array<T, N>& arr, VariadicFunc func) {
return methodApply(arr, func, Indices());
}
template <typename T, std::size_t N>
auto method(const std::array<T, N>& arr) {
return methodArr(arr, [](const auto&... args) { return method(args...); });
}
int main() {
method(1.0f, 2.0f, 3.0f);
std::array<float, 4> arr{1.0f, 2.0f, 3.0f};
method(arr);
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.