Why does everybody typedef over standard C types?

前端 未结 4 1896
执笔经年
执笔经年 2020-12-12 23:19

If you want to use Qt, you have to embrace quint8, quint16 and so forth.

If you want to use GLib, you have to welcome

相关标签:
4条回答
  • 2020-12-12 23:39

    For the older libraries, this is needed because the header in question (stdint.h) didn't exist.

    There's still, however, a problem around: those types (uint64_t and others) are an optional feature in the standard. So a complying implementation might not ship with them -- and thus force libraries to still include them nowadays.

    0 讨论(0)
  • 2020-12-12 23:49

    stdint.h didn't exist back when these libraries were being developed. So each library made its own typedefs.

    0 讨论(0)
  • 2020-12-13 00:04

    So you have the power to typedef char to int.

    One "coding horror" mentioned that one companies header had a point where a programmer wanted a boolean value, and a char was the logical native type for the job, and so wrote typedef bool char. Then later on someone found an integer to be the most logical choice, and wrote typedef bool int. The result, ages before Unicode, was virtually typedef char int.

    Quite a lot of forward-thinking, forward compatibility, I think.

    0 讨论(0)
  • 2020-12-13 00:06

    stdint.h has been standardised since 1999. It is more likely that many applications define (effectively alias) types to maintain partial independence from the underlying machine architecture.

    They provide developers confidence that types used in their application matches their project specific assumptions on behavior that may not match either the language standard or compiler implementation.

    The practice is mirrored in the object oriented Façade design pattern and is much abused by developers invariably writing wrapper classes for all imported libraries.

    When compliers were much less standard and machine architectures could vary from 16-bit, 18-bit through 36-bit word length mainframes this was much more of a consideration. The practice is much less relevant now in a world converging on 32-bit ARM embedded systems. It remains a concern for low-end microcontrollers with odd memory maps.

    0 讨论(0)
提交回复
热议问题