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();
是的,你的理解是正確的。變量列表根據用法自動擴展,因此,空變量列表完全等同於「無」。 –