【问题标题】:Designing programs to compile on both 32bit and 64bit in D设计程序以在 D 中编译 32 位和 64 位
【发布时间】:2016-12-03 09:07:21
【问题描述】:

我有一个开源项目,我想在 32 位和 64 位架构上运行/编译。但是问题来了:它大量使用动态数组,并且还需要弄乱它们的长度。动态数组的长度存储在 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
}

【问题讨论】:

  • 你想要ptrdiff_t。就像 size_t 刚刚签名一样。
  • 如果它总是合适的话,为什么不显式转换呢?
  • @SamiKuhmonen 那是因为在 64 位上使用 uint 是无稽之谈,我希望能够利用 64 位编译的优势。
  • 所以不是“这必须是 int”,而是“必须签名”。为什么?
  • @RichardAndrewCattermole 这就是我要找的东西,谢谢。请将此作为答案发布,以便我接受。

标签: 64-bit d 32-bit cpu-architecture conditional-compilation


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-18
      • 1970-01-01
      • 2011-04-12
      • 2012-01-02
      • 2020-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多