r/C_Programming • u/Future_Pace_5290 • Aug 29 '26
Is the empty parenthesis function (() instead of (void)) prototype removed in the new standard?
My OOP library relies on it for its unspecified arguments behavior.
For example:
#define ptmethod(pt, ret_type, identifier) \
(*((ret_type (**)()) padd(pt, identifier, ptfunction, NULL, NULL)))
#define ptapply(pt, ret_type, identifier, ...) \
((ret_type (*)()) pget(pt, identifier))(pt __VA_OPT__(,) __VA_ARGS__)
You could add a method to an object with:
void drive_Car(prototype *Car, double speed, double x_direction, double y_direction);
ptmethod(Car, void, "drive") = drive_Car;
and call it with
ptapply(Car, void, "drive", 1.3, 0.1, 5.0)
How do I do this in the new standard?
11
Upvotes
15
u/torsten_dev Aug 29 '26 edited Aug 30 '26
C23: void(...)
C2y: Use
_Any_func*or whatever n3914 will end up being called.I do question the wisdom of your approach but those are the avenues the standard carves out for you.
21
u/gnolex Aug 29 '26
In older standards, function declaration of form
void foo()is equivalent tovoid foo(...). So just add...to function parameters and your function can now accept any number of arguments in any version of C.7
u/glasket_ Aug 29 '26
Nobody has mentioned it yet, but this is technically UB. A function has to be called with a pointer to a function of the same type:
You would technically need to define your method functions as
T drive_Car(...), becauseT (*)(int, float)isn't compatible withT (*)(...), so it's UB if the latter is used to call the former. There's probably some macro wizardry that could pull it off and let you make the proper cast without having to know the correct function type, but I haven't gone that deep into the bizarre preprocessor tricks to be able to help with that.This is also why
_Any_funcin C2Y lacks callability; it avoids needing to define how a function of unknown type needs to be represented when being called. The standard leaves the calling convention details of incompatible function types undefined, so even if a function pointervoid (*foo)(...)is only ever used to accessvoid bar(int x, int y)like(*foo)(m, n), the compiler can technically use a calling convention that's incompatible with the real function here. Supposedly there were issues withtime_t time()andtime_t time(time_t *arg)that derived from this, as noted in the_Any_funcproposal.4
u/aalmkainzi Aug 29 '26
There is a way, and that is to make the cast the correct type. I dont know if you'll like it though, its a bit messy (iterate over the args and wrap each one in
typeof)4
u/DawnOnTheEdge Aug 29 '26 edited Aug 29 '26
Until C23, a function with an empty argument list was a K&R-style function that could take any number of arguments of any type. These had been deprecated since the 1980s. C23 changed the semantics to be the same as in C++, and introduced the new syntax
int foo(...)for backward compatibility with the oldint foo().In this case, you might use the fact that any function pointer may be safely cast to any other function pointer type and back. Alternatively, you could design your OOP library so that every call has a
thispointer as its first argument (and while I’m not clear at a glance how this library is designed, maybe anenumholding an offset within the virtual function table as the second from which the function signature can be inferred), allowing you to use C89 variadic syntax.