Việc dựa vào việc triển khai các kích thước kiểu nguyên thủy của nhà cung cấp trình biên dịch SẼ quay lại ám ảnh bạn nếu bạn từng biên dịch mã của mình trên một kiến trúc máy khác, hệ điều hành hoặc trình biên dịch của nhà cung cấp khác.
Hầu hết các nhà cung cấp trình biên dịch cung cấp một tệp tiêu đề xác định các kiểu nguyên thủy với kích thước kiểu hết hạn. Những kiểu nguyên thủy này nên được sử dụng khi mã có thể được chuyển sang một trình biên dịch khác (đọc là LUÔN LUÔN trong MỌI trường hợp). Ví dụ, hầu hết các trình biên dịch UNIX đều có int8_t uint8_t int16_t int32_t uint32_t
. Microsoft có INT8 UINT8 INT16 UINT16 INT32 UINT32
. Tôi thích Borland / CodeGear's int8 uint8 int16 uint16 int32 uint32
. Những tên này cũng nhắc nhở một chút về kích thước / phạm vi của giá trị dự định.
Trong nhiều năm, tôi đã sử dụng tên kiểu nguyên thủy rõ ràng của Borland và #include
tệp tiêu đề C / C ++ sau đây (nguyên thủy.h) nhằm xác định các kiểu nguyên thủy rõ ràng với những tên này cho bất kỳ trình biên dịch C / C ++ nào (tệp tiêu đề này có thể không thực sự bao gồm mọi trình biên dịch nhưng nó bao gồm một số trình biên dịch mà tôi đã sử dụng trên Windows, UNIX và Linux, nó cũng không (chưa) xác định các loại 64bit).
#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