2016-02-07 80 views
2
struct Tuple(Types...){ 
    Types values; 
    this(Types types){ 
     import std.algorithm.mutation; 
     foreach(index, ref t; types){ 
      values[index] = move(t); 
     } 
    } 
    alias values this; 
} 
auto tuple(Ts...)(Ts ts){ 
    import std.algorithm.mutation; 
    static if(Ts.length == 0){ 
     return Tuple!Ts(ts); // This is the problem 
    } 
    else{ 
     return unpack!(move).into!(Tuple!Ts)(ts); 
    } 
} 
static template unpack(alias f){ 
    pragma(inline) 
    auto into(alias target, Args...)(auto ref Args args){ 
     import std.conv; 
     import std.algorithm; 
     import std.range; 
     enum s = `target(`~iota(Args.length).map!(i=>text(`f(args[`,i,`])`)).join(",")~`)`; 
     return mixin(s); 
    } 
} 

爲什麼我可以寫無法缺省的init自定義類型

auto t = Tuple!Foo(); 
// but not 
auto t1 = tuple(); 

的錯誤是

構造meta.Tuple!()。對於結構Tuple.this默認構造函數只允許與@disable,沒有身體,也沒有參數

但它不會消失如果我@disable this()。另外std.typecons.Tuple也不會這樣做,它似乎工作得很好。

auto t3 = std.typecons.tuple(); 

回答

1
struct Tuple(Types...){ 
    Types values; 
    alias values this; 
    alias expand = values; 
    static if(Types.length > 0){ 
     this(Types types){ 
      import std.algorithm.mutation; 
      foreach(index, ref t; types){ 
       values[index] = move(t); 
      } 
     } 
    } 
} 

構造是問題。可能是因爲它會導致this()如果Types.length == 0,這是不允許的。

+0

是的,你的理解是正確的。變量列表根據用法自動擴展,因此,空變量列表完全等同於「無」。 –

0

根本問題是tuple而不是一個類型:它只是一個幫助函數,它返回一個基於它的參數的類型。既然你沒有提供參數,它沒有有效的類型返回,因此無法編譯。

+0

這是不正確的。無參數的'tuple'調用是合法的,它返回類型爲'Tuple!()'的類型。 –