Search code examples
64-bitd32-bitcpu-architectureconditional-compilation

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


I have an open source project that I want to run/compile on both 32 bit and 64 bit architectures. But here comes the problem: it uses dynamic arrays a lot, and needs to mess with their length too. Dynamic arrays have length stored in type size_t, which is ulong on 64bit, and uint on 32 bit systems.
I have code that looks something like this:

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

I need i to int (on 32bit) and long on (64bit). size_t would have solved the problem, but it is unsigned (either uint or ulong).

So my question here is that: how do I create a integer data type that is int on 32 bit and long on 64 bit? Will it be something like this?:

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

Solution

  • As discussed in comments, there's already a signed pointer-width integer type. It's called ptrdiff_t in D (and C and C++). Don't define your own.