Saturday, 15 January 2011

c - Cause compilation error in C89 if two types are not the same -



c - Cause compilation error in C89 if two types are not the same -

using features of c89, given

typedef [unspecified token sequence] t1; typedef [another unspecified token sequence] t2;

exhibit language build compile without error if , if t1 , t2 the same type (not compatible). limitation c89 because going autoconf probe.

edit: need solution works if t1 or t2 or both incomplete types. sorry not mentioning before.

son of edit: 3 current answers observe compatible types. turns out much closer "the same type" remembered, close plenty current purposes, out of curiosity still looking reply detects the same type. here pairs of types compatible not same:

typedef void (*t1)(void); typedef void (*t2)(); typedef float t1[]; typedef float t2[12]; typedef enum { one, two, 3 } t1; typedef /* implementation-defined integer type */ t2;

i think should able utilize strict type checking of extern declarations:

typedef int t1; typedef char t2; extern t1 t1; t2 t1;

the above not compile. changing t2 int allow source build correctly.

this not compile:

typedef int t1; typedef unsigned int t2; extern t1 t1; t2 t1;

even though both types int. think want.

however, not trigger error:

typedef emum {foo} t1; typedef unsigned t2;

so it's not 100% waterproof. however, 1 has maintain in mind there's nil 1 can enum can't done unsigned. have same layout , can used interchangeably. effectively, same type.

c types autoconf

No comments:

Post a Comment