2
编译时是否可以将Foo
转换为ubyte[size]
?在编译时可以将Foo转换为ubyte [size]吗?
这是一个有点背景:
struct Algebraic(Types...)
if(Types.length < char.max - 1){
import std.traits: Largest;
import std.meta: IndexOf;
static immutable maxSize = Largest!(Types).sizeof;
this(T)(in T t)
if(IndexOf!(T, Types) !is -1){
type = IndexOf!(T, Types);
data = *cast(ubyte[maxSize]*)&t;
}
void opAssign(T)(in T t)
if(IndexOf!(T, Types) !is -1){
type = IndexOf!(T, Types);
data = *cast(ubyte[maxSize]*)&t;
}
inout(T*) peek(T)() inout{
if(type is IndexOf!(T, Types)){
return cast(inout(T*))&data;
}
return null;
}
private:
ubyte[maxSize] data;
char type = char.max;
}
struct Branch{
int index;
int left;
int right;
}
struct Leaf{
int index;
}
struct Foo{
alias Node = Algebraic!(Branch, Leaf);
Node n = Branch(1,2,3);
//Error: cannot convert &const(Branch) to ubyte[12]* at compile time
}
的问题是,我不能在编译时投Branch
到ubyte[maxSize]
。