Macros can be useful but I feel like we should ban certain people from touching them.
For example, imagine if someone were to use the C preprocessor to create an elaborate object orientation system for C, calling it GObject, and then create an entire graphics stack up to and including a desktop environment, called, say, GNOME.
I think that's the kind of person who should no longer be allowed to use the C preprocessor.
@jordyd btw. what do you think about macros used in kernel, like container_of, or in BSD, eg. the TAILQ ones?
@jordyd
struct myStruct {
int myStuff;
linked_list_node node;
};
struct myStruct *s;
linked_list_node *n1 = s->node.next;
s = container_of(myStruct, node, n1);
@Wolf480pl At least as far as C goes
@jordyd
(I might have written some things in a different order than in kernel, I've only seen it once or twice).
It does some pointer arithmetic under the hood, and I think doing it by hand every time would be more error-prone than using the macro.
@jordyd It's used a lot in the kernel, because they always embed linked list nodes within the structs that they want to keep on the list.
@Wolf480pl a small, short, and often-repeated bit of code is exactly the kind of thing macros are good at
@Wolf480pl especially given C has no generics (well there’s _Generic in C11 but it’s not incredibly powerful) and functions declared inline aren’t always inlined
@Wolf480pl A bit strange but it doesn't appear overly complex or error-prone