I think the motivation for claiming that varargs functions must have a named parameter is the uniformity of va_start . For ease of implementation, va_start takes the name of the last named parameter. With a typical convention of calling varargs and depending on the direction arguments, va_arg will find the first vararg at (¶meter_name) + 1 or (first_vararg_type*)(¶meter_name) - 1 , plus or minus some addition to ensure alignment.
I don’t think there is any special reason why the language cannot support varargs functions without named parameters. For use in such functions, there must be an alternative form of va_start , which would have to get the first vararg directly from the stack pointer (or be a pedantic frame pointer, which is actually the value that the stack pointer was included in the function, since the code in the function could move sp from the moment the function is entered). This is possible in principle - any implementation must have access to the [*] stack in some way, at some level - but this may annoy some developers. Once you know the varargs calling convention, you can usually implement va_ macros without any other implementation-specific knowledge, and this also requires knowing how to access the call arguments directly. I implemented these varargs macros before, in the emulation layer, and that would annoy me.
In addition, there is not much practical use for a varargs function without named parameters. There is no language function for the varargs function to determine the type and number of variable arguments, so the caller must know the type of the first vararg in any case in order to read it. Thus, you can also make it a named parameter with a type. In printf and friends, the value of the first parameter tells the function what types belong to varargs, and how many of them are.
I believe that in theory the caller could look at some global question, how to understand the first argument (and whether there is one), but this is rather unpleasant. Of course, I would not be going to come to terms with this, and adding a new version of va_start with an additional burden for downloading will be on my part.
[*], or if the implementation does not use the stack, regardless of what it uses instead, to pass arguments to the function.
source share