It's good software engineering practice to minimize gratuitous portability problems in the code. Techniques to minimize potential portability problems are:
a + b + c
can be evaluated as (a + b) + c, a + (b + c), (a + c) + b, (c + b) + a, etc. Parentheses control operator precedence, parentheses do not control order of evaluation.
If the operands of an associative operator + or * are floating point values, the expression is not reordered.
static assert
in the code to verify it: static assert(int.sizeof == (int*).sizeof);
64 bit processors and operating systems are here. With that in mind:
size_t
as an alias for an unsigned integral type that can span the address space. Array indices should be of type size_t
.ptrdiff_t
as an alias for a signed integral type that can span the address space. A type representing the difference between two pointers should be of type ptrdiff_t
..length
, .size
, .sizeof
, .offsetof
and .alignof
properties will be of type size_t
.Endianness refers to the order in which multibyte types are stored. The two main orders are big endian and little endian. The compiler predefines the version identifier BigEndian
or LittleEndian
depending on the order of the target system. The x86 systems are all little endian.
The times when endianness matters are:
long
s or double
s.System specific code is handled by isolating the differences into separate modules. At compile time, the correct system specific module is imported.
Minor differences can be handled by constant defined in a system specific import, and then using that constant in an IfStatement or StaticIfStatement.
© 1999–2019 The D Language Foundation
Licensed under the Boost License 1.0.
https://dlang.org/spec/portability.html