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?
@Wolf480pl I'm not familiar with container_of but the BSD ones I think are mostly fine. When they were invented there wasn't really any readily-available alternative for that kind of functionality. GObject, on the other hand, could have been written using Objective-C, which had been around for quite some time by the time GNOME was created.
@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 A bit strange but it doesn't appear overly complex or error-prone
@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.
@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