|
[Quotation]
Promote cross-platform development
Typedef has another important purpose, which is to define machine-independent types. For example, you can define a floating-point type called REAL, which can achieve the highest precision on the target machine:
typedef long double REAL;
On machines that do not support long double, the typedef will look like this:
typedef double REAL;
And, on machines that do not even support double, the typedef will look like this:,
typedef float REAL;
You can compile this application that uses REAL type on every platform without any modification to the source code. The only thing to change is the typedef itself. In most cases, even this slight change can be achieved automatically by wonderful conditional compilation. Isn't it? The standard library uses typedef extensively to create such platform-independent types: size_t, ptrdiff and fpos_t are examples. In addition, typedefs such as std :: string and std :: ofstream hide long, difficult-to-understand template specialization syntax, such as basic_string, allocator> and basic_ofstream>. |
|