C++ Logo

std-proposals

Advanced search

Re: [std-proposals] requires keyword (new use)

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Sun, 1 May 2022 12:59:53 -0400
On Sun, May 1, 2022 at 4:15 AM Abdullah Qasim via Std-Proposals
<std-proposals_at_[hidden]> wrote:
> int menu (/**/requires/**/ unsigned int menu_option = 0)
>
>
>
> // Called as:
>
> menu(default);
>
>
>
> // OR:
>
> menu(5)
>
>
>
> // Compiler Error: function "menu(unsigned int)" requires //unsigned int variable
>
> menu()
>
>
>
> I do not want calls like menu(), but I do want ability to call menu(default), as, in this case, going to default screen makes perfect sense.

So, you don't want the purpose of default parameters (ie: being able
to not provide an argument for that parameter), but you want certain
effects of default parameters (ie: having the function decide what the
parameter is).

So just take an `optional`:

```
int menu (std::optional<unsigned int> menu_option)
{
  if(!menu_option) menu_option = 0;
...
}
menu(std::nullopt);
menu(5);
menu(); //Does not work.
```

Also, please don't spam the list with dozens of "proposals" like this.
We would like proposals to be more considered than "here's an idea I
had". C++ is a rich and varied language with many tools. You need to
take time to understand what those tools are and how to use them
*before* you can effectively start suggesting what tools the language
ought to have.

Received on 2022-05-01 17:00:42