C++ Logo

std-discussion

Advanced search

Re: Explicit and implicit parameter counts

From: David Brown <david.brown_at_[hidden]>
Date: Mon, 6 Sep 2021 13:28:35 +0200
On 03/09/2021 16:48, Rick C. Hodgin via Std-Discussion wrote:
> Greetings.
>
> Since we can define explicitly required parameters, and implicitly
> required parameters (populated with default values), I'd like to
> propose a way to know the number of each provided to a function by its
> caller.
>

You can get this using function overloads, rather than default
parameters. (Some people consider default parameters to be a bad idea,
and that you should always use overloads - I'm not sure I agree
entirely, but that argument has merit.)

> This information would be available for use in two ways depending on
> how it's referenced:
>
> (1) As add-on named function parameters for use as use variables (for
> output, storage, or passing as parameters to other functions). These
> would be auto-added to the function's parameters by the compiler,
> though invisible to the user unless an examination of the stack was
> made:
>
> (2) As compile-time definitions which allow for conditional
> compilation, which would ultimately create versions of a function
> based on provided counts and the subsequent reference-only logic.
>
> -----[BEGIN: Example for type (1) ]-----
>
> // Prototype
> int func(int a, int b = 0, int c = 0);
>
> // Use in code:
> int main()
> {
> func(0);
> func(0, 0);
> func(0, 0, 0);
> return 0;
> }
>
> // Body:
> int func(int a, int b, int c)
> {
> // In theory, it would use two auto-injected compiler-added
parameters:
> std::cout << "Explicit count = " << __ARGS_EXPLICIT__ << std::endl;
> std::cout << "Implicit count = " << __ARGS_IMPLICIT__ <<
> std::endl; << std::endl;
> }
>
> The above would be used as parameters producing output with counts of:
> 1 explicit, 2 implicit
> 2 explicit, 1 implicit
> 3 explicit, 0 implicit
>
> This feature would be the equivalent of translating the actual
> function definition into something like this by the compiler:
> int func(int __ARGS_EXPLICIT__, int __ARGS_IMPLICIT__, int a, int b =
> 0, int c = 0);
>
> With auto-injected additional parameters added by the compiler like this:
> func(1, 2, 0);
> func(2, 1, 0, 0);
> func(3, 0, 0, 0, 0);

int func_implementation(int args_explicit, int a, int b, int c);
int inline func(int a)
 { return func_implementation(1, a, 0, 0); }
int inline func(int a, int b)
 { return func_implementation(2, a, b, 0); }
int inline func(int a, int b, int c)
 { return func_implementation(3, a, b, c); }


If that is going to be too much boiler plate for you, a variadic
template should cover it. And you have a place for any specialisation
for the different numbers of explicit arguments.

Received on 2021-09-06 06:28:41