直接回答问题 - 使用 align(N) 指定所需的对齐方式。但请记住这一点(引自dlang.org): Do not align references or pointers that were allocated using NewExpression on boundaries that are not a multiple of size_t
align(N)
Do not align references or pointers that were allocated using NewExpression on boundaries that are not a multiple of size_t
参考手册在 http://dlang.org 有关于对齐的部分 - http://dlang.org/attribute.html#align 。
比方说,T是int - 边缘!int在64位架构上已经是24字节大。 D与C的对齐方式没有那么不同。检查以下内容(可运行/可编辑代码: http://dpaste.dzfl.pl/de0121e1 ):
module so_0001; // http://stackoverflow.com/questions/11383240/custom-alignment-options-for-an-efficient-quad-edge-implementation struct edge(T) { edge* next; T* data; uint position; } alias edge!int iedge; alias edge!int[4] qedge; /* not a good idea for struct... it is a value type... edge!int new_edge() { // in the original example it used new... D is not C++ to mix value and reference types! :) } */ import std.stdio; int main() { writeln(iedge.sizeof); // 64bit // ---------- writeln(iedge.next.offsetof); // 0 writeln(iedge.data.offsetof); // 8 writeln(iedge.position.offsetof); // 16 writeln(qedge.sizeof); return 0; }