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

后端 未结 9 2451
遇见更好的自我
遇见更好的自我 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:07

    In C, the only distinction between user-defined types that is enforced by the compiler is the distinction between structs. Any typedef involving distinct structs will work. Your major design question is should different struct types use the same member names? If so, you can simulate some polymorphic code using macros and other scurvy tricks. If not, you are really committed to two different representations. E.g., do you want to be able to

    #define INCREMENT(s, k) ((s).n += (k))
    

    and use INCREMENT on both byte_idx and char_idx? Then name the fields identically.

提交回复
热议问题