Am 05.10.2018 um 21:08 schrieb Jeff King: > On Fri, Oct 05, 2018 at 08:48:27PM +0200, René Scharfe wrote: >> +#define DEFINE_SORT(name, type, compare) \ >> +static int compare##_void(const void *one, const void *two) \ >> +{ \ >> + return compare(one, two); \ >> +} \ >> +static void name(type base, size_t nmemb) \ >> +{ \ >> + const type dummy = NULL; \ >> + if (nmemb > 1) \ >> + qsort(base, nmemb, sizeof(base[0]), compare##_void); \ >> + else if (0) \ >> + compare(dummy, dummy); \ >> +} > > I do like that this removes the need to have the code block aspart of > the macro. > > Did you measure to see if there is any runtime impact? No, but I wouldn't expect any -- the generated code should be the same in most cases. Here's an example: https://godbolt.org/z/gwXENy. > As an aside, we may need to take a "scope" argument in case somebody > wants to do this in a non-static way. Sure. (They could easily wrap the static function, but a macro parameter is simpler still.) > It would be nice if we could make > this "static inline", but I don't think even a clever compiler would be > able to omit the wrapper call. It could, if it was to inline qsort(3). Current compilers don't do that AFAIK, but I wouldn't be too surprised if they started to. The typed comparison function can be inlined into the one with the void pointers, though. René