C++ Logo

STD-PROPOSALS

Advanced search

Subject: [std-proposals] Non-trainling default function arguments
From: Avi Kivity (avi_at_[hidden])
Date: 2020-02-02 10:36:45


Currently, default arguments to functions must be trailing; "void f(int
x = 4, int y)" is not allowed.

I propose to relax this. The main motivation is that a trailing lambda
argument is readable, while a lambda argument inside a longer parameter
list is not.

Readable:

 Â Â Â  f(5, foo, bar, [baz] {

 Â Â Â Â Â Â Â  baz->shoo();

 Â Â Â  });

Less readable:

 Â Â Â  f(5, [baz] {

 Â Â Â Â Â Â Â  baz->shoo();

 Â Â Â  }), foo, bar);

So, there is competition for the last slot of the argument list.

To implement this, the compiler generates overloads for each default
parameter combination.

 Â Â Â  void f(int x = 4, int y);

is translated to

 Â Â Â  void f(int x, int y);

 Â Â Â  void f(int y) { f(4, std::forward<int>(y)); }

Functions with many non-trailing default arguments may generate many
combinations; some of them may be ambiguous, which the compiler should
diagnose.


STD-PROPOSALS list run by herb.sutter at gmail.com

Standard Proposals Archives on Google Groups