在我之前的question之后,我现在无法使用可变参数覆盖opIndex。我尝试了多种方法(甚至是黑客方法),但无济于事。
我用来生成标识符字符串的代码
static string array_decl(D...)(string identifier, D dimensions)
{
static if(dimensions.length == 0)
{
return identifier;
}
else
{
return array_decl(identifier ~ "[" ~ to!(string)(dimensions[0]) ~ "]", dimensions[1..$]);
}
}
我的opIndex覆盖是什么样的:
T opIndex(D...)(D indices)
{
mixin("return " ~ array_decl("Data", indices) ~ ";");
}
失败:
./inheritance.d(81): Error: tuple D is used as a type
./inheritance.d(89): Error: template instance inheritance.array_ident!(int, int, int).array_ident.array_ident!(_param_2, _param_3) error instantiating
./inheritance.d(112): instantiated from here: array_ident!(int, int, int)
./inheritance.d(174): instantiated from here: opIndex!(int, int, int)
./inheritance.d(112): Error: CTFE failed because of previous errors in array_ident
./inheritance.d(112): Error: argument to mixin must be a string, not ("return " ~ array_ident("Data", _param_0, _param_1, _param_2) ~ ";") of type string
问题是如何(或可能)为这种情况实现opIndex运算符。
我认为mixins是要走的路,因为我只需要生成格式为的字符串:
type[index0][index1]...[indexN] Data
表示opIndex重载。
答案 0 :(得分:1)
显然这是不可能的,因为传递给opIndex的元组在编译时是不可访问的。我提出了一些解决方案(由Adam D. Ruppe建议):
使用编译时条件索引数组,有点难看,可以访问的维数取决于实现的条件数量。
T opIndex(D...)(D indices)
{
static if(indices.length == 1)
{
return Data[indices[0]];
}
static if(indices.length == 2)
{
return Data[indices[0]][indices[1]];
}
static if(indices.length == 3)
{
return Data[indices[0]][indices[1]][indices[2]];
}
static if(indices.length == 4)
{
return Data[indices[0]][indices[1]][indices[2]][indices[3]];
}
}
唯一的另一种方法是将数组转换为指针然后使用偏移量。计算偏移量,然后用于索引指针。
能够在运行时访问模板参数:
struct Vector_MultiDim(T, D...)
{
enum dimensions = [D];
static const size_t DimCount = D.length;
... Other members here
}
计算偏移量的函数(每个维度的大小必须在运行时知道):
size_t GetIndex(size_t[] indices)
{
size_t index;
for(size_t i = 0; i < DimCount; i++)
{
size_t factor = 1;
for(size_t j = i + 1; j < DimCount; j++)
{
factor *= dimensions[j];
}
index += indices[i] * factor;
}
return index;
}
opIndex覆盖:
T opIndex(D...)(D indices)
{
T* arr = cast(T*)Data;
return arr[GetIndex([indices])];
}