1

In my work I work with many different compilers for many platforms (Windows, embedded microcontrollers, Arduino, etc). Now I want to write a generic routine that should work with all of them, but I'm getting conflicts with data types. It's mostly low-level stuff, so I would like to work with types like 'byte', 'word', 'bool' etc.

For some compilers these types are not yet defined, but for some they are and in these cases that will result in errors of conflicting types.

I have learned that typedef are prefered above #define. And in this question it is made clear that there is no way to make a conditional typedef.

I already thought of using unique types like for example:

typedef unsigned char mybyte
typedef unsigned short int myword
etc...

But that would make my sourcecode look very ugly IMHO.

Hneel
  • 95
  • 10
  • 1
    the only chance to define different types in function of architecture is by using conditional #define – alinsoar Jun 19 '19 at 09:30
  • 5
    Portability is a big topic. Regarding types specifically, you should be using `stdint.h` and pick the type for each variable with care. Porting code containing small integer types to 8 bit MCUs takes quite a lot of C skill - why I recommend all beginners to stay clear of crap like Arduino. – Lundin Jun 19 '19 at 09:30
  • See also [BYTE, WORD and DWORD macros definition](https://stackoverflow.com/q/47697926/2173773) – Håkon Hægland Jun 19 '19 at 09:31
  • Oh and also, keeping code portable between C and C++ is another major pain... so you might want to settle for pure C, as that gets significantly easier to port. Except then you can't port to Arduino... – Lundin Jun 19 '19 at 09:42
  • 2
    It is impossible and doesn't make sense to support _every platform_. Your code is never "portable" or "not-portable", it is always portable to some degree which can be measured as a number of supported platforms. You need to understand what platforms you need to support before you start writing "portable" code. –  Jun 19 '19 at 09:44
  • 2
    First, decide if you are using C or C++ - coding to a "common subset" of the two makes portability harder to achieve. Use standard types (built-in or in the standard library) where you can, or types that can be created using them (structures, pointers, etc) - but account for implementation-defined properties. Failing that, use types that the standard specifies, but that are not guaranteed for all compilers (e.g. `int64_t`). If you must define types differently for different compilers, do it sparingly, and use the preprocessor to detect each compiler and do definitions accordingly. – Peter Jun 19 '19 at 09:49
  • 3
    What do you expect from a type called `word`? – eerorika Jun 19 '19 at 10:19
  • @eeronika - personally, I'd expect to be able to check variables of that type with a spell-checker. ;-) – Peter Jun 19 '19 at 10:33

1 Answers1

5

All platforms should support bool as it is a reserved keyword for a built-in type in C++.

The only platform I know of that has byte and word is Arduino. They are just typedef'ed aliases to uint8_t and unsigned int respectively. (Source)

If you have existing Arduino code that uses byte and word, the easiest solution would be to check if your code runs in the Arduino environment, and define the Arduino types yourself if that's not the case:

#ifdef ARDUINO
#include <Arduino.h>
#else
#include <cstdint>
typedef uint16_t word; // or unsigned int, depending on your needs
typedef uint8_t byte;
#endif

However, my preferred solution is to just use the standard integers of stdint.h directly when I need a specific number of bits. Using byte and word just adds to the confusion, because it is non-standard. uint16_t tells you exactly what it is, you know exactly what the largest possible value is, and whether it's signed or not.

tttapa
  • 1,397
  • 12
  • 26