Спираючись на реалізацію примітивом типових розмірів постачальника компілятора, ЗНО буде переслідувати вас, якщо ви коли-небудь компілюєте свій код на іншій архітектурі машини, ОС або іншому компіляторі постачальника.
Більшість постачальників компіляторів надають файл заголовка, який визначає примітивні типи з розмірами типу explict. Ці примітивні типи слід використовувати, коли коли-небудь код може бути потенційно перенесений на інший компілятор (читайте це ЗАВЖДИ в кожному випадку). Наприклад, у більшості компіляторів UNIX є int8_t uint8_t int16_t int32_t uint32_t
. Microsoft має INT8 UINT8 INT16 UINT16 INT32 UINT32
. Я віддаю перевагу Borland / CodeGear's int8 uint8 int16 uint16 int32 uint32
. Ці назви також трохи нагадують про розмір / діапазон призначеного значення.
Протягом багатьох років я використовував явні прімітивні назви типу Borland та #include
наступний заголовок C / C ++ (primitive.h), який призначений для визначення явних примітивних типів із цими іменами для будь-якого компілятора C / C ++ (цей файл заголовка може насправді не охоплювати кожного компілятор, але він охоплює декілька компіляторів, які я використовував у Windows, UNIX та Linux, він також не (ще) не визначає 64-ти бітні типи).
#ifndef primitiveH
#define primitiveH
// Header file primitive.h
// Primitive types
// For C and/or C++
// This header file is intended to define a set of primitive types
// that will always be the same number bytes on any operating operating systems
// and/or for several popular C/C++ compiler vendors.
// Currently the type definitions cover:
// Windows (16 or 32 bit)
// Linux
// UNIX (HP/US, Solaris)
// And the following compiler vendors
// Microsoft, Borland/Imprise/CodeGear, SunStudio, HP/UX
// (maybe GNU C/C++)
// This does not currently include 64bit primitives.
#define float64 double
#define float32 float
// Some old C++ compilers didn't have bool type
// If your compiler does not have bool then add emulate_bool
// to your command line -D option or defined macros.
#ifdef emulate_bool
# ifdef TVISION
# define bool int
# define true 1
# define false 0
# else
# ifdef __BCPLUSPLUS__
//BC++ bool type not available until 5.0
# define BI_NO_BOOL
# include <classlib/defs.h>
# else
# define bool int
# define true 1
# define false 0
# endif
# endif
#endif
#ifdef __BCPLUSPLUS__
# include <systypes.h>
#else
# ifdef unix
# ifdef hpux
# include <sys/_inttypes.h>
# endif
# ifdef sun
# include <sys/int_types.h>
# endif
# ifdef linux
# include <idna.h>
# endif
# define int8 int8_t
# define uint8 uint8_t
# define int16 int16_t
# define int32 int32_t
# define uint16 uint16_t
# define uint32 uint32_t
# else
# ifdef _MSC_VER
# include <BaseTSD.h>
# define int8 INT8
# define uint8 UINT8
# define int16 INT16
# define int32 INT32
# define uint16 UINT16
# define uint32 UINT32
# else
# ifndef OWL6
// OWL version 6 already defines these types
# define int8 char
# define uint8 unsigned char
# ifdef __WIN32_
# define int16 short int
# define int32 long
# define uint16 unsigned short int
# define uint32 unsigned long
# else
# define int16 int
# define int32 long
# define uint16 unsigned int
# define uint32 unsigned long
# endif
# endif
# endif
# endif
#endif
typedef int8 sint8;
typedef int16 sint16;
typedef int32 sint32;
typedef uint8 nat8;
typedef uint16 nat16;
typedef uint32 nat32;
typedef const char * cASCIIz; // constant null terminated char array
typedef char * ASCIIz; // null terminated char array
#endif
//primitive.h