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

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

    You could do something like:

    typedef struct {
        unsigned int c_idx;
    } char_idx;
    
    typedef struct {
        unsigned int b_idx;
    } byte_idx;
    

    Then you would see when you are using each:

    char_idx a;
    byte_idx b;
    
    b.b_idx = a.c_idx;  
    

    Now it is more clear that they are different types but would still compile.

提交回复
热议问题