在 D 中设计程序以在 32 位和 64 位上编译

Designing programs to compile on both 32bit and 64bit in D

我有一个开源项目,我想在 32 位和 64 位架构上 run/compile。但是问题来了:它大量使用动态数组,并且也需要弄乱它们的长度。动态数组的长度以 size_t 类型存储,在 64 位系统上为 ulong,在 32 位系统上为 uint
我的代码看起来像这样:

int i = 0;//this HAS to be int, not uint for some reasons
i = dynArray.length;//error, can't implicitly cast ulong to uint

我需要 iint(32 位)和 long(64 位)。 size_t 本来可以解决问题,但它是 unsigneduintulong)。

所以我的问题是:如何创建在 32 位上为 int 而在 64 位上为 long 的整数数据类型?会是这样吗?:

32bit{
   //Declaration for 32 bit version
}else{
   //Declaration for 64 bit version
}

试试这个:

version (X86)
{
   // Declaration for 32 bit version
   alias myint = int;
}
else
{
   // Declaration for 64 bit version
   alias myint = long;
}

正如评论中所讨论的,已经有一个带符号的指针宽度整数类型。它在 D(以及 C 和 C++)中称为 ptrdiff_t。不要定义你自己的。