无法默认初始化自定义类型
Unable to default init a custom type
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);
}
}
为什么我可以写
auto t = Tuple!Foo();
// but not
auto t1 = tuple();
错误是
constructor meta.Tuple!().Tuple.this default constructor for structs only allowed with @disable, no body, and no parameters
但如果我 @disable this()
它不会消失。另外 std.typecons.Tuple
也没有这样做,它似乎工作得很好。
auto t3 = std.typecons.tuple();
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);
}
}
}
}
问题出在构造函数上。可能是因为如果 Types.length == 0
会导致 this()
,这是不允许的。
根本问题是 tuple
不是 类型:它只是一个辅助函数,return 是一个基于其参数的类型。由于您没有提供任何参数,因此它没有 return 的有效类型,因此无法编译。
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);
}
}
为什么我可以写
auto t = Tuple!Foo();
// but not
auto t1 = tuple();
错误是
constructor meta.Tuple!().Tuple.this default constructor for structs only allowed with @disable, no body, and no parameters
但如果我 @disable this()
它不会消失。另外 std.typecons.Tuple
也没有这样做,它似乎工作得很好。
auto t3 = std.typecons.tuple();
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);
}
}
}
}
问题出在构造函数上。可能是因为如果 Types.length == 0
会导致 this()
,这是不允许的。
根本问题是 tuple
不是 类型:它只是一个辅助函数,return 是一个基于其参数的类型。由于您没有提供任何参数,因此它没有 return 的有效类型,因此无法编译。