18 ms·
You seem to have changed your mind while writing your response, and you appear to have come to the same conclusion I did. Personally, when I learned about _Gen
by maxlybbert 11y ago
You seem to have changed your mind while writing your response, and you appear to have come to the same conclusion I did.
Personally, when I learned about _Generic, I wondered why the C committee hadn't just adopted C++-style overloading. I'm convinced the reason is to avoid name mangling. The end result is that the programmer does the name mangling
"by hand," with natural consequences for doing so.
However, there are some important points, which make _Generic both more flexible than C++ overloading in some ways, and less flexible in other ways:
* _Generic only dispatches based on a single parameter.
* _Generic doesn't have to expand to a function call; it could expand to a format string, for instance (although if a macro, FOO, expands to a string literal you can't put FOO(X) next to another string literal and expect the compiler to join the two strings).
* _Generic doesn't consider type conversions when doing the dispatching.
* _Generic dispatching can be as complex as the programmer wishes (e.g., _Generic(X), switch (sizeof(X)) ...); yes, you start with type, but you don't have to limit yourself to exact matches.
* _Generic can have a "default:" branch; this is possible in C++ but not obvious (have one overload take "..." to guarantee it will be the worst match, and only chosen when there is no other option).