Using only the features of C89, given
typedef [unspecified token sequence] T1;
typedef [another unspecified token sequence] T2;
exhibit a language construct which will compile without error if and only if T1 and T2 are the same type (not just compatible). The limitation to C89 is because this is going into an autoconf probe.
EDIT: I need a solution which works even if T1 or T2 or both are incomplete types. Sorry for not mentioning this before.
SON OF EDIT: All three current answers only detect compatible types. This turns out to be much closer to "the same type" than I remembered, close enough for my current purposes, but out of curiosity I am still looking for an answer that detects the same type. Here are some pairs of types that are compatible but not the same:
typedef void (*T1)(void);
typedef void (*T2)();
typedef float T1[];
typedef float T2[12];
typedef enum { ONE, TWO, THREE } T1;
typedef /* implementation-defined integer type */ T2;
-Werror
, a warning will become an error, so that is probably fine. – Crepusculeint
vsunsigned
. But no. Those are actually not compatible. For all intents and purposes I can think of, compatible types can be treated as equal. So why is type compatibility not good enough in your case? – Liviaextern "C"
-tagged. Name mangling uses a stricter definition of 'the same type' than C89's 'compatible'; for instance, in yourenum
vsint
example, T1 and T2 would mangle differently. I don't want to have to have a C++ compiler around for the library build just to detect this kind of type mismatch (it would be the only thing it was used for). – Ensphereenum
vs. underlying integer type is the only scenario liable to come up in practice, and for the specific thing I'm doing, it shouldn't happen. – Ensphere