Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • D

Idiom #160 Detect if 32-bit or 64-bit architecture

Execute f32() if platform is 32-bit, or f64() if platform is 64-bit.
This can be either a compile-time condition (depending on target) or a runtime detection.

version(X86)
    f32();
version(X86_64)
    f64();

version() is a compile-time condition.
static if (size_t.sizeof == 4)
    f32();
static if (size_t.sizeof == 8)
    f64();

Using a static if condition, which is evaluated at compile-time.
if constexpr(sizeof(nullptr) == 8) {
  f64();
} else {
  f32();
}

This tests the size of a pointer.

New implementation...
< >
programming-idioms.org