如何创建范围的元组? 模板tupIndexToRange(别名Tup,标记…){ 导入标准元数据; 输入标准类型; 静态if(Indicies.length==0){ enum tupIndexToRange=tuple(); } 否则{ enum tupIndexToRange=tuple(Tup[Indicies[0]][],tupIndexToRange!(Tup,Indicies[1..$]); } } void main(){ alias Integrals=AliasSeq!(数组!int,数组!float,数组!double); 元组!积分; 积分[0]。回插(1); 积分[1]。回插(2); 积分[2]。回插(3); 自动t=tupIndexToRange!(积分,0,1,2); 自动r=zip(t.expand); }

如何创建范围的元组? 模板tupIndexToRange(别名Tup,标记…){ 导入标准元数据; 输入标准类型; 静态if(Indicies.length==0){ enum tupIndexToRange=tuple(); } 否则{ enum tupIndexToRange=tuple(Tup[Indicies[0]][],tupIndexToRange!(Tup,Indicies[1..$]); } } void main(){ alias Integrals=AliasSeq!(数组!int,数组!float,数组!double); 元组!积分; 积分[0]。回插(1); 积分[1]。回插(2); 积分[2]。回插(3); 自动t=tupIndexToRange!(积分,0,1,2); 自动r=zip(t.expand); },d,D,错误: 我假设出现此错误是因为我试图在编译时访问该范围?我如何告诉D我只需要在运行时访问范围 这就是tupIndexToRange应该做的: auto t=tuple(积分[0][],积分[1][],积分[2][]); 我如何告诉D我只需要在运行时访问范围 通过将其作为运行时参数传递: template tupIndexToRange(Indicies...) { auto tupIndexToRange(T)(T tup) { static if(Indicies.len

错误:

我假设出现此错误是因为我试图在编译时访问该范围?我如何告诉D我只需要在运行时访问范围

这就是
tupIndexToRange
应该做的:

auto t=tuple(积分[0][],积分[1][],积分[2][]);
我如何告诉D我只需要在运行时访问范围

通过将其作为运行时参数传递:

template tupIndexToRange(Indicies...) {
    auto tupIndexToRange(T)(T tup) {
      static if(Indicies.length == 0){
        return tuple();
      }
      else{
        return tuple(tup[ Indicies[0] ][], .tupIndexToRange!(Indicies[1..$])(tup).expand);
      }
    }
}
有两点需要注意:

  • 模板中嵌套的函数允许我们只指定索引而不是类型。如果它只是
    auto-tupIndexToRange(T,index)(T-tup)
    ,我们就必须显式地提供类型

  • 我们通过调用
    .tupIndexToRange
    而不是
    tupIndexToRange
    进行递归,因为我们希望通过外部作用域中的模板而不是嵌套函数进行递归


  • 虽然理论上可以使用别名。。。我很想知道是否有人能想出解决这个问题的办法。
    template tupIndexToRange(Indicies...) {
        auto tupIndexToRange(T)(T tup) {
          static if(Indicies.length == 0){
            return tuple();
          }
          else{
            return tuple(tup[ Indicies[0] ][], .tupIndexToRange!(Indicies[1..$])(tup).expand);
          }
        }
    }