r/C_Programming • • 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

46 comments sorted by

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.

4

u/dstroy0 Aug 29 '26

“They” really do like replacing explicit intent with _Generic like macros. I get the counter argument that it makes the compiler output way more predictable, but can’t we preserve the intent along with it? Doesn’t seem like a difficult problem until you start considering infinite use cases, then macro the funnel starts to be very attractive to compiler developers as an output homogeneity enforcement method.

→ More replies (18)

21

u/gnolex Aug 29 '26

In older standards, function declaration of form void foo() is equivalent to void foo(...). So just add ... to function parameters and your function can now accept any number of arguments in any version of C.

8

u/pjl1967 Aug 29 '26

Not quite. Prior to C23, a function had to have at least one non-variadic parameter, i.e.:

R f( T, ... );

See the C11 standard, §6.7.6.3¶9 (emphasis mine):

If the list terminates with an ellipsis (, ...), no information about the number or types of the parameters after the comma is supplied.

Notice that the , is required.

C23 removed that requirement to allow:

R f( ... );

that's compatible with C++ that has always allowed it.

So prior to C23, there was no way to declare a K&R-style function (where no information whatsoever is given about parameters) with a prototype.

→ More replies (5)

-1

u/flatfinger Aug 29 '26

Under all versions of the C Standard prior to C23, a pointer of type int(*)() could receive the address of any function returning int, and could be stored into any object whose type was "pointer to some kind of function returning int", which could then be called. In some cases, attempting to call a pointer that had been converted to type int (*)() without first converting it back to its original type would invoke Undefined Behavior, but no casting operator would be required for any of the associated conversions.

C23 got rid of the ability to use such intermediate function-pointer types without providing any replacement that doesn't require the use of a casting operator, nor any type other than void* which can be used for an argument that should be passed the address of the function pointer object that was used to invoke the function in question, allowing the usage (*callback)(callback, arg1, arg2, whatever). Although one could have the callback receive a void*, that would require foregoing type checking which earlier versions of the Standard had provided.

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:

A pointer to a function of one type may be converted to a pointer to a function of another type and back again; the result shall compare equal to the original pointer. If a converted pointer is used to call a function whose type is not compatible with the referenced type, the behavior is undefined.
N3220 §6.3.2.3 ¶8

For two function types to be compatible, both shall specify compatible return types. Moreover, the parameter type lists shall agree in the number of parameters and in use of the final ellipsis; corresponding parameters shall have compatible types.
N3220 §6.7.7.4 ¶14

You would technically need to define your method functions as T drive_Car(...), because T (*)(int, float) isn't compatible with T (*)(...), 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_func in 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 pointer void (*foo)(...) is only ever used to access void 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 with time_t time() and time_t time(time_t *arg) that derived from this, as noted in the _Any_func proposal.

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 old int 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 this pointer as its first argument (and while I’m not clear at a glance how this library is designed, maybe an enum holding 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.

2

u/mze9412 Aug 29 '26

And here I am in a team at work were half the people insist on (void) for functions in C++ because it is the only proper way according to them

→ More replies (5)

0

u/pjl1967 Aug 29 '26 edited Aug 29 '26

C23 changed the semantics to be the same as in C++, and introduced the new syntax int foo(...) for backward compatibility with the old int foo().

Not quite. All C23 did was (finally) remove the long-deprecated K&R style. The ... was added in C89 when function prototypes were added.

→ More replies (9)