With a pointer you have complete (input/output) signature of the function so a reasonable compiler can take a constant pointer and lower to fixed function call (or even inline it) (visual-c fails here AFAIK) which gives overriding. The internal memory mangers of WebKit mimic traits by doing this.
As long as it's not gratuitous and confusing and a possible trap, it's a good feature.
Much time was lost due to attempts to use macros for generic code like even min/max (you can write them to evaluate once but only using compiler extensions afaik).
Stuff like + already is overloaded (compared to ASM or OCaml). For pointers it's adding sizeof(*ptr) not the given number and can be UB, for ints it can be UB, for unsigned it won't be UB and safely wraps, for float and double it's a completely different instruction and register (x87, FPU, SSE ones, whatever depending on platform) so not even same silicone engaged. This can affect stuff majorly: very old computers might lack fpu or emulate it (slowly), Quake code intermingled int and float operations on purpose, AMD bulldozer had (sort of...) 2 int cores share one floating unit, which led to a lawsuit over advertised core count.
Same for calling functions, a f() is a function call and will do right stack and register operations for calling convention function is declared with, and f can even bea function pointer (so possibly dangling or null and crashing, or doing different things at different times if someone sets it to a new value) and not a function, and you can't tell at the call site unless you enforce dereference in style guide or linter (which can lead you to other issues, like false positives in many GL loaders that use global function pointers). This is on top of compiler choosing jump vs call instruction, which has implications for stack frames, or that it can inline the function if it's local or must call it though (on Linux) global table (possibly loading it) if not.
A - can be used as binary or unary.
You're just used to these and they're a closed group but it's in practice a form of overloading (doing different thing depending on type or amount of arguments) too.
I've seen solutions for this problem before, and they are not as elegant. Instead of using _Generic chaining, the idea of creating a function pointer to "switch on" is pretty clean.
This is a matter of taste, of course, but I think this code is pretty readable.
Are you thinking for __VA_OPT__ ? Hasn't that been supported for quite awhile in all mainstream compilers? (Might need to add a switch for visual-c though). The __VA_OPT__ can be stripped out but that kills zero argument support w/o additional special casing.
(EDIT: I wasn't really thinking with my question was I?)
hehe honestly, I'm very surprised by the MSVC support for __VA_OPT__. Now it's still not standard-compliant until C23, but still, this makes me more confident to depend on this feature.
Too many negative comments; function "overloading" should not be abused, but it is sometimes appropriate. This implementation is actually nice because it handles variadic overload (including no parameter) and type overload in a simple single interface.
3
u/pjl1967 Jun 11 '26
This technique has been around a while. Personally,
constvs. non-constoverloading is generally more useful to me (which is also possible in C).5
u/Stemt Jun 11 '26
I dont know about this, what is the advantage? I think that depending an the situation it would be alot simpler to just use a function pointer.
2
u/vitamin_CPP Jun 11 '26 edited Jun 11 '26
This is great.
I've seen solutions for this problem before, and they are not as elegant. Instead of using
_Genericchaining, the idea of creating a function pointer to "switch on" is pretty clean.This is a matter of taste, of course, but I think this code is pretty readable.
EDIT:
It also handles
atan()correctly (no args)!Do you think it would be possible to make it work with C11? Maybe with this : Advanced C Preprocessor Macros for a Pre-C23/C++20 __VA_OPT__ Substitute | Medium
16
u/florianist Jun 11 '26
Too many negative comments; function "overloading" should not be abused, but it is sometimes appropriate. This implementation is actually nice because it handles variadic overload (including no parameter) and type overload in a simple single interface.
8
u/richtw1 Jun 11 '26
It's a C++ feature I personally miss in C, but I wouldn't jump through those hoops to try to get it back.
People often accuse C++ of being overly verbose, but what's verbose to me is having to somehow restate the function signature in its name.
I also miss proper generics but that's a different discussion.
2
u/This_Growth2898 Jun 11 '26
Just waiting for him to discover <tgmath.h> (since C99)
2
u/bullno1 Jun 11 '26
This is cursed, I like it. Although, most of the time, I'd just write
_Genericsince I don't have differing arity.One of the most useful thing with
_Genericis type-generic and expression-safe min/max/clamp. The macro version would evaluate the expression twice.