1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <array>
namespace indexer_detail {
template <class T>
struct ArrayWrapper {
T obj;
};
template <class T>
ArrayWrapper(T&&) -> ArrayWrapper<T&&>;
template <class T>
auto operator & (ArrayWrapper<T> const &aw, std::size_t N) {
return ArrayWrapper{aw.obj[N]};
}
}
template <std::size_t Size, class Array, std::size_t... Idx>
decltype(auto) index(Array &&array, std::array<std::size_t, Size> const &indices, std::index_sequence<Idx...>) {
return (indexer_detail::ArrayWrapper{std::forward<Array>(array)} & ... & indices[Idx]).obj;
}
template <std::size_t Size, class Array>
decltype(auto) index(Array &&array, std::array<std::size_t, Size> const &indices) {
return index(std::forward<Array>(array), indices, std::make_index_sequence<Size>{});
}
int main() {
int arr[1][2][3] {{{1}}};
std::cout << index<3>(arr, {{0, 0, 0}}) << index<3>(arr, {{0, 1, 0}}) << '\n';;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#clang++ -std=c++1z -Wall -Wextra -Wfatal-errors -pedantic-errors -stdlib=libc++ main.cpp && ./a.out
g++ -std=c++1z -Wall -Wextra -Wfatal-errors -pedantic-errors main.cpp && ./a.out
10