C++ Logo

std-proposals

Advanced search

Non-trainling default function arguments

From: Avi Kivity <avi_at_[hidden]>
Date: Sun, 2 Feb 2020 18:36:45 +0200
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.

Received on 2020-02-02 10:39:24