If you plan on using primitive data types, then your original macro-based solution seems pretty elegant. However, when you start storing pairs of pointers on opaque data types with complex structures below them that are intended to be used by specifying pointers between functions, for example:
complex_structure_type *object = complex_structure_type_init(); complex_structure_type_set_title(object, "Whatever"); complex_structure_type_free(object);
then you should
typedef complex_structure_type *complex_structure_type_ptr;
so that
DEFINE_PAIR(complex_structure_type_ptr);
so you can
Pair_complex_structure_type_ptr p;
and then
px = object;
But it only works a little more, so if you feel that it works for you, go for it. You can even build your own preprocessor that goes through the code, pulls out something like Pair_whatever, and then adds DEFINE_PAIR (whatever) for the C preprocessor. Anyway, this is definitely a great idea that you presented here.
Personally, I would just use void pointers and forget about strict type security. C simply does not have the same type of security technique as other languages, and the more opportunities you give yourself to forget something, the more errors you accidentally create.
Good luck
James O'Doherty
source share