Warn if another typedef'd name of a type is used in an argument list

后端 未结 6 1537
天涯浪人
天涯浪人 2021-01-02 09:46

Consider a large project, where many types are typedef\'d, e.g.

typedef int age;
typedef int height;

and some functions gettin

6条回答
  •  猫巷女王i
    2021-01-02 10:21

    As others already stated, there is no support for this in C. If you absolutely want strong type checking to happen, you could do like this:

    typedef struct {int a;} age;
    typedef struct {int h;} height;
    
    void printPerson(age a, height h)
    {
        printf("Age %d, height %d\n", a.a, h.h);
    }
    
    age a = {30};
    height h = {180};
    
    printPerson(h, a);  // will generate errors
    

    Beware that this might have some performance impact, though.

提交回复
热议问题