Skip to content

Instantly share code, notes, and snippets.

@aguinet
Created April 11, 2017 06:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aguinet/845b285b688f7478bac5813066be111f to your computer and use it in GitHub Desktop.
Save aguinet/845b285b688f7478bac5813066be111f to your computer and use it in GitHub Desktop.
#include <array>
#include <cstdio>
template <class Array, class F, class Seq>
struct map_impl;
template <class Array, class F, size_t... I>
struct map_impl<Array, F, std::index_sequence<I...>>
{
template <class... Args>
static constexpr Array run(Array const A, Args... args)
{
return Array{F::value(A[I], args...)...};
}
};
template <class F, class T, size_t N, class... Args>
constexpr auto map(std::array<T,N> const A, Args... args)
{
return map_impl<std::array<T,N>, F, decltype(std::make_index_sequence<N>())>::run(A, args...);
}
constexpr int f(int i) { return i+1; }
int main()
{
constexpr std::array<int, 10> A = {0};
constexpr auto B = map<std::integral_constant<decltype(&f), &f>>(A);
for (int V: B) {
printf("%d\n", V);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment