C++ Logo

std-proposals

Advanced search

Re: Video Intro to WG21/D2288 C++ Designated Arguments

From: Hani Deek <Hani_deek_at_[hidden]>
Date: Tue, 23 Nov 2021 00:57:50 +0000
> Right. And for "but I just want to pass two ints", it doesn't actually
> seem like that much of a burden to write
> void f(struct {int a = 42; int b = 666;} oink = {});

> and just use oink.a and oink.b in the function body.

To avoid having to use oink.a and oink.b in the function body, one can formulate a simple rule: if the type of a function parameter is a class type that is defined in the parameter list of that function, then the function body can access the parameter members directly, without having to use the member access operator. So you can write just 'a' and 'b' instead of 'oink.a' and 'oink.b.'

Such a rule wouldn't be completely strange to C++. Something similar already exists in the non-static member functions, where we can access members of the 'implicit object parameter' without having to use the member access syntax, i.e. we write 'a' as a shorthand for 'this->a'.

A name ambiguity issue can arise if parameters of this sort have members with identical names to other parameters or to members of other parameters. This is not a big deal. A simple solution can be formulated (e.g. require the user to explicitly write 'oink.a' in case there is ambiguity about the name 'a').

Probably a more consistent solution would be to find some generalized syntax (possibly some keyword) that makes it possible to import the member names of any parameter of class type into the function parameter scope, regardless of where the parameter type was declared. This is preferable because we can use the generalized syntax to import the names of an 'explicit object parameter' into the function body in case we were writing a definition of a member function with an 'explicit object parameter' rather than the usual 'implicit object parameter'.

For example, if we use the keyword 'using' to import names into the function body, we can declare a function as follows.

void f(using struct {int a = 0; int b = 0;});

The presence of the keyword 'using' (or whatever keyword to be proposed) means that the names inside the parameter type are accessible in the function body.

Similarly, a non-static member function with an 'explicit object parameter' may be defined as follows.

void T::f(this using T const&);

The presence of the keyword 'using' means that the names of the type T are accessible in the function body, exactly as if we were writing an old non-static member function with an 'implicit object parameter'.

Of course, that last example won't be really a definition of a non-static member function under the C++23 draft. Unfortunately, that syntax is now reserved for a new type of member functions that is introduced in C++23 per the proposal "Deducing this" (P0847R). As far as I understand, there is currently no possibility in C++ to write a non-static member function with an 'explicit object parameter'. Hopefully someone will propose syntax to make that possible.



Received on 2021-11-22 18:57:54