这应该可以,但是您必须使用data[1][2][3] 而不是indexed(data,1,2,3)
它适用于plain arrays 和std::arrays。您可以通过复制专业化来扩展它std::vector。 (我认为它应该适用于任何重载 operator[] 但不确定的东西。)
#include <iostream>
#include <array>
template<typename T, size_t dim>
struct getTypeAtDim { typedef T type; };
template<typename T, size_t N>
struct getTypeAtDim<T[N],1> { typedef T type; };
template<typename T, size_t dim, size_t N>
struct getTypeAtDim<T[N],dim> : getTypeAtDim< T, dim-1> {};
template<typename T, size_t N>
struct getTypeAtDim<std::array<T,N>,1> { typedef T type; };
template<typename T, size_t dim, size_t N>
struct getTypeAtDim<std::array<T,N>,dim> : getTypeAtDim< T, dim-1> {};
template<typename T, size_t dim>
using typeAtDim = typename getTypeAtDim<T, dim>::type;
template<typename T>
typeAtDim<T,1>&
indexed(T& arr, const int& first) {
return arr[first];
}
template<typename T, typename... Args>
typeAtDim<T,sizeof...(Args) + 1>&
indexed(T& arr, const int& first, const Args& ...rest) {
return indexed(arr[first],rest...);
}
int main() {
std::array<int,2> a1 = {1,2};
std::array<int,2> a2 = {3,4};
std::array<std::array<int,2>,2> a = {a1,a2};
std::array<std::array<std::array<int,2>,2>,2> c = {a,a};
int b[2][2] = {{5,6},{7,8}};
std::cout << indexed(a,1,1) << std::endl;
indexed(a,1,1) = 5;
std::cout << indexed(a,1,1) << std::endl;
std::cout << indexed(b,1,1) << std::endl;
std::cout << indexed(c,1,1,1) << std::endl;
indexed(c,1,1) = a1;
std::cout << indexed(c,1,1,1) << std::endl;
}
4 5 8 4 2
Here is a test run.
我没有将auto 与trailing return types 一起使用,因为indexed 的可变参数版本在推断返回类型时不会匹配自身。因此,在 gcc 解决之前,您将不得不使用类似的东西。