Created
April 11, 2017 06:34
-
-
Save aguinet/845b285b688f7478bac5813066be111f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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