I am developing a system where users register their functions with a framework that calls the functions on the users' behalf. The framework accepts user functions that have at least one function parameter, which helps discourage functions with too many side effects. The number of input parameters to a function is thus checked at compile time. My code for getting the number of input parameters is simple:
template <typename R, typename... Args>
constexpr std::size_t number_input_parameters(R (*)(Args...)) { return sizeof...(Args); }
and then using it:
int no_parameters() {} // would be rejected by the framework
static_assert(number_input_parameters(no_parameters) == 0);
While developing the code, I was worried about functions that have void
parameters:
int still_no_parameters(void) {} // framework should also reject this
but to my delight, the above implementation of number_of_parameters
gives the right answer (using Clang 15 and GCC 12):
static_assert(number_input_parameters(still_no_parameters) == 0);
See https://godbolt.org/z/Taeox1rMq.
So clearly a function with a type R(void)
is correctly interpreted by the compiler as having type R()
. The link above also shows this to be true without using templates.
The question: In what way does the C (20) standard somehow specify that a void
function parameter should be ignored? I've looked in various places and been unsuccessful.
CodePudding user response:
In what way does the C (20) standard somehow specify that a void function parameter should be ignored?
From https://eel.is/c draft/dcl.dcl#dcl.fct-4 :
A parameter list consisting of a single unnamed parameter of non-dependent type void is equivalent to an empty parameter list.