Last active
March 25, 2017 17:09
-
-
Save merryhime/b755268263a9466cee657b301c3120a0 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 <cstddef> | |
#include <type_traits> | |
template <typename T, std::size_t N, std::size_t... Ns> | |
struct multi_array | |
{ | |
using type = std::array<typename multi_array<T, Ns...>::type, N>; | |
}; | |
template <typename T, std::size_t N> | |
struct multi_array<T, N> | |
{ | |
using type = std::array<T, N>; | |
}; | |
template <typename T, std::size_t... Ns> | |
using multi_array_t = typename multi_array<T, Ns...>::type; | |
template <typename T> | |
struct array_element | |
{ | |
using type = T; | |
}; | |
template <typename T, std::size_t N> | |
struct array_element<std::array<T, N>> | |
{ | |
using type = typename array_element<T>::type; | |
}; | |
template <typename T> | |
using array_element_t = typename array_element<T>::type; | |
template <std::size_t... DestShape> | |
struct reshape_array | |
{ | |
template <typename Src, size_t N1> | |
static auto reshape(std::array<Src, N1>& src) -> multi_array_t<array_element_t<Src>, DestShape...>& | |
{ | |
static_assert(sizeof(std::array<Src, N1>) == sizeof(multi_array_t<array_element_t<Src>, DestShape...>)); | |
return *reinterpret_cast<multi_array_t<array_element_t<Src>, DestShape...>*>(&src); | |
} | |
}; | |
int main(int argc, char** argv) | |
{ | |
std::array<int, 4> foo {1, 2, 3, 4}; | |
std::array<std::array<int, 2>, 2>& bar = reshape_array<2, 2>::reshape(foo); | |
bar[1][1] = 0; | |
for (int f : foo) | |
{ | |
printf("%i\n", f); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment