Are macros needed for generics in C?
24, Sep, 2018Generic code in C mostly look like this:
#define MAP(BAR) {
for (int i = 0; i < ...; ++i) {
BAR(i);
}
}
But with little work it may look like that:
static inline void map(void (*bar)(int)) {
for (int i = 0; i < ...; ++i) {
bar(i);
}
}
But then question arises. If you call like this:
map(&foo);
will the compiler optimize foo and call it directly?
Yes (tested gcc and clang -O2). Even more - in my test, it inlined body of foo also…
- Posted by michal in
- Add a comment