I have a function that sorts a list of pointers to structures (implementation omitted):
void sort_by(struct thing **start, int size,
int (*cmp_by)(struct thing *, struct thing *));
This function allows me to pass in a comparison function cmp_by
that takes two structures and returns 1
, 0
, -1
as appropriate. Then there exists this (GCC-only!) function:
void reverse_by(struct thing **start, int size,
int (*cmp_by)(struct thing *, struct thing *)) {
int cmp_reverse(struct thing *a, struct thing *b) {
return -cmp_by(a, b);
}
sort_by(start, size, cmp_reverse);
}
This function, takes the same arguments as sort_by
but reverses the sort order by flipping the sign of the result of comparison. GCC has this extension to allow nested functions. How should reverse_by
be written portably? Even my attempts at getting this to work come nowhere near to compiling:
int (*reverse(int (*fn)(struct thing *, struct thing *)))(struct thing *, struct thing *) {
return -fn; /* OBVIOUSLY AND CONCEPTUALLY WRONG */
}
int (*reverse(int (*fn)(struct thing *a, struct thing *b)))(struct thing *x, struct thing *y) {
return -fn(a, b); /* WRONG RETURN TYPE WRONG WRONG */
}
int (*reverse(int (*fn)(struct thing *a, struct thing *b)))(struct thing *x, struct thing *y) {
return -fn(x, y); /* YOU HAVE NO IDEA WHAT YOU'RE DOING STOP GUESSING */
}
Short of adding a lot of extra structure parts or rewriting swaths of code calling reverse_by
(or copy-pasting sort_by
code into it), how can this be rewritten to eliminate the nested function? It would be good to avoid copy/paste as a solution because this code base involves this nested-function-pattern elsewhere.