Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a C++ embedded application use a common header with typedefs for built-in C++ types?

It's common practice where I work to avoid directly using built-in types and instead include a standardtypes.h that has items like:

// \Common\standardtypes.h
typedef double             Float64_T;
typedef int                SInt32_T;

Almost all components and source files become dependent on this header, but some people argue that it's needed to abstract the size of the types (in practice this hasn't been needed).

Is this a good practice (especially in large-componentized systems)? Are there better alternatives? Or should the built-in types be used directly?

like image 753
Brandon Leiran Avatar asked Dec 03 '22 11:12

Brandon Leiran


2 Answers

You can use the standardized versions available in modern C and C++ implementations in the header file: stdint.h

It has types of the like: uint8_t, int32_t, etc.

In general this is a good way to protect code against platform dependency. Even if you haven't experienced a need for it to date, it certainly makes the code easier to interpret since one doesn't need to guess a storage size as you would for 'int' or 'long' which will vary in size with platform.

like image 117
Amardeep AC9MF Avatar answered May 19 '23 07:05

Amardeep AC9MF


It would probably be better to use the standard POSIX types defined in stdint.h et al, e.g. uint8_t, int32_t, etc. I'm not sure if there are part of C++ yet but they are in C99.

like image 23
Paul R Avatar answered May 19 '23 05:05

Paul R