Enforce strong type checking in C (type strictness for typedefs)

后端 未结 9 2371
遇见更好的自我
遇见更好的自我 2020-11-29 03:43

Is there a way to enforce explicit cast for typedefs of the same type? I\'ve to deal with utf8 and sometimes I get confused with the indices for the character count and the

相关标签:
9条回答
  • 2020-11-29 04:09

    Use a lint. See Splint:Types and strong type check.

    Strong type checking often reveals programming errors. Splint can check primitive C types more strictly and flexibly than typical compilers (4.1) and provides support a Boolean type (4.2). In addition, users can define abstract types that provide information hiding (0).

    0 讨论(0)
  • 2020-11-29 04:09

    With C++11 you can use an enum class, e.g.

    enum class char_idx_t : unsigned int {};
    enum class byte_idx_t : unsigned int {};
    

    The compiler will enforce an explicit cast between the two types; it is like a thin wrapper class. Unfortunately you won't have operator overloading, e.g. if you want to add two char_idx_t together you will have to cast them to unsigned int.

    0 讨论(0)
  • 2020-11-29 04:13

    If you were writing C++, you could make two identically defined classes with different names that were wrappers around an unsigned int. I don't know of a trick to do what you want in C.

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