www.digitalmars.com Home | Search | D | Comments
Last update Fri Aug 26 2005
D
Language
Phobos
Comparisons

· Lexical
· Modules
· Declarations
· Types
· Properties
· Attributes
· Pragmas
· Expressions
· Statements
· Arrays
· Structs & Unions
· Classes
· Interfaces
· Enums
· Functions
· Operator Overloading
· Templates
· Mixins
· Contracts
· Conditional Compilation
· Handling errors
· Garbage Collection
· Memory Management
· Floating Point
· Inline Assembler
· Interfacing To C
· Portability Guide
· Embedding D in HTML
· Named Character Entities
· Application Binary Interface

Portability Guide

It's good software engineering practice to minimize gratuitous portability problems in the code. Techniques to minimize potential portability problems are:
  • The integral and floating type sizes should be considered as minimums. Algorithms should be designed to continue to work properly if the type size increases.
  • Floating point computations can be carried out at a higher precision than the size of the floating point variable can hold. Floating point algorithms should continue to work properly if precision is arbitrarily increased.
  • Avoid depending on the order of side effects in a computation that may get reordered by the compiler. For example:
    	a + b + c
    	
    can be evaluated as (a + b) + c, a + (b + c), (a + c) + b, (c + b) + a, etc. Parenthesis control operator precedence, parenthesis do not control order of evaluation.

    In particular, function parameters can be evaluated either left to right or right to left, depending on the particular calling conventions used.

    If the operands of an associative operator + or * are floating point values, the expression is not reordered.

  • Avoid dependence on byte order; i.e. whether the CPU is big-endian or little-endian.
  • Avoid dependence on the size of a pointer or reference being the same size as a particular integral type.
  • If size dependencies are inevitable, put an assert in the code to verify it:
    	assert(int.size == (int*).size);
    	

32 to 64 Bit Portability

64 bit processors and operating systems are coming. With that in mind:
  • Integral types will remain the same sizes between 32 and 64 bit code.
  • Pointers and object references will increase in size from 4 bytes to 8 bytes going from 32 to 64 bit code.
  • Use size_t as an alias for an unsigned integral type that can span the address space.
  • Use ptrdiff_t as an alias for a signed integral type that can span the address space.
  • The .length, .size, .sizeof, and .alignof properties will be of type size_t.

OS Specific Code

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 if statement.

Feedback and Comments

Add feedback and comments regarding this page.