C++ Logo

std-proposals

Advanced search

Re: Flexible function parameters

From: Nikolay Mihaylov <nmmm_at_[hidden]>
Date: Thu, 9 Jul 2020 21:53:38 +0300
My 5 cents

In python, there is a way to "reorder" function parameters. I do not like
it, but it looks something like this:

int f(int a = 5, int b = 5);

then you call like this:

f(1,2); // standard way
f(1); // standard way
f(b = 2); // a is default, b is 2
f(b = 2, a = 1); // a is 1, b is 2

in C / C++ you can do similar with struct:

struct X{
  int a = 5;
  int b = 5;
};

int f(X p = {});

int main(){
  f();
  f({ .b = 4 });
}

https://gcc.godbolt.org/z/YxdzT7

Never used it myself, but lots of good programmers said this is priceless
if you have 5+ parameters...

N



On Thu, Jul 9, 2020 at 6:06 PM Filippo Casarin via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> I haven't explained what it does in the first mail so i will explain now.
>
> This proposal introduces more ways to overload a function.
>
> What is `void f(int x, int y) {}` supposed to do differently from `void
>> f(int x; int y) {}` or `void f(int x: int y) {}`?
>
> In this case for the machine there is no difference other than the name
> mangling.
>
> Why is it useful?
>
> 1.
> Given the code
>
>> void foo(int a=1000696967, int b=1000696969);
>>
> In c++20 there is no way to call `foo` specifying a value for `b` but not
> for `a`
>
> With this proposal you will be allowed to do it in the following way
>
>> void foo(int a=1000696967; int b=1000696969);
>> foo(; 7); // calling foo with default valute for `a` and `b` = 7
>>
>
> 2.
> In c++20 you can't have multiple parameters packs in a function, the only
> solution I know is from the std::pair constructor but it's too long to call
>
>> std::pair<std::complex<double>, std::string> p6( // from
>> https://en.cppreference.com/w/cpp/utility/pair/pair
>> std::piecewise_construct,
>> std::forward_as_tuple(0.123, 7.7),
>> std::forward_as_tuple(10, 'a'));
>>
>
> The new syntax would be
>
>> std::pair<std::complex<double>, std::string> p6(0.123, 7.7; 10, 'a');
>>
>
> 3.
> The colon has been included just for the slice because other possibilities
> didn't look as good
>>
>> arr[i:j] // slicing an array
>>
> arr[i, j] // indexing a matrix
>>
> arr[i; j] // does not mean anything to me
>>
>
>
>
>
> Il giorno gio 9 lug 2020 alle ore 16:02 Arthur O'Dwyer via Std-Proposals <
> std-proposals_at_[hidden]> ha scritto:
>
>> On Thu, Jul 9, 2020 at 9:32 AM Jake Arkinstall via Std-Proposals <
>> std-proposals_at_[hidden]> wrote:
>>
>>> The why is clear.
>>>
>>
>> FWIW, no it's not. The OP didn't give any reason for the new syntax at
>> all. In fact, the OP didn't even say what the new syntax is supposed to
>> *do!*
>> What is `void f(int x, int y) {}` supposed to do differently from `void
>> f(int x; int y) {}` or `void f(int x: int y) {}`? Explaining *what the
>> change does* is an important part of any change request.
>>
>>
>>> It's a way of forming groupings of parameters, which currently isn't
>>> possible without bringing structs into the equation. This opens up some
>>> decent possibilities: Defaults can apply to the group (rather than having
>>> to put them at the end of the call), parameter packs can be logically
>>> separated in a way they currently cannot, and so we can pass multiple
>>> parameters to operator[] without interfering with the comma operator.
>>>
>>
>> FYI, there's already a deploy plan in motion (to the admittedly limited
>> extent that WG21 can be said to make "deploy plans") to get multi-parameter
>> `operator[]` overloads. The first step in the plan was committed in C++20:
>> http://eel.is/c++draft/depr.comma.subscript
>> The second step, targeted for C++23, is to support overloaded
>> `operator[](int x, int y)` (or whatever parameter types) with the natural
>> meaning and call syntax.
>> The goal and justification, targeted also for C++23 as far as I know, is
>> to finally ship `mdspan`. (Which I personally think is a silly end goal,
>> but there seem to be a lot of people looking forward to it just as a lot of
>> people look forward to C++20's `span`.)
>>
>> –Arthur
>> --
>> Std-Proposals mailing list
>> Std-Proposals_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2020-07-09 13:57:30