Wolf480pl is a user on niu.moe. You can follow them or interact with them if you have an account anywhere in the fediverse. If you don't, you can sign up here.

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 @Wolf480pl

@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.