C++ Logo

std-proposals

Advanced search

Re: Parameters with default values at arbitrary positions, with empty arguments

From: Alejandro Colomar <une+cxx_std-proposals_at_[hidden]>
Date: Wed, 2 Dec 2026 22:17:08 +0100
Hi!

Here's a draft of a proposal for default arguments at arbitrary
positions, using the 'default' keyword (it had more support than empty
arguments).

Below it is in plain text. Attached is an HTML version with a colored
diff.


Have a lovely night!
Alex

---
Name
 alx-0104r0 - explicit default arguments, at arbitrary positions
Author
 Alejandro Colomar <alx_at_[hidden]>
History
 <https://www.alejandro-colomar.es/src/alx/alx/wg21/alx-0104.git/>
 r0 (2026-12-02):
 -  Initial draft.
Description
 Default arguments are a bit too limited.  In some cases, one may
 want to use them in non-trailing parameters.  Or one may want to
 be more explicit that a default argument is invoked.
 This proposal allows using the 'default' keyword to invoke a
 default argument, which would be explicit, and that explicitness
 would itself allow using default arguments in non-trailing
 parameters, as they wouldn't be ambiguous.
 Here's a simple example of how this feature would work:
  int f(int a = 6, int b);
  f(default, 7);  // Evaluates as f(6,7);
 Some programmers currently use overloads for implementing this,
 but that's heavier machinery, and more ambiguous (and thus less
 safe and/or robust against simple mistakes).
Proposed wording
 Based on C++ draft N5054.
    7.6.1.1  Postfix expressions :: General
 @@ p1
  expression-list:
   initializer-list
 + <tt>default</tt>
 + expression-list , expression-list
    7.6.1.3  Function call
 @@ p7
  When a function is called,
  each parameter (9.3.4.6)
  is initialized (9.5, 11.4.5.3)
  with its corresponding argument,
  and each precondition assertion of the function call
  is evaluated (9.4.1).
  If the function is an explicit object member function
  and there is an implied object argument (12.2.2.2.2),
  the list of provided arguments
  is preceded by the implied object argument
  for the purposes of this correspondence.
  If there is no corresponding argument,
 +or the corresponding argument is the <tt>default</tt> keyword,
  the default argument for the parameter is used.
 +If the corresponding argument is the <tt>default</tt> keyword
 +and the parameter has no default argument,
 +the program is ill-formed.
    9.3.4.7  Default arguments
 @@ p1, Note 1
  Default arguments will be used in calls
  where trailing arguments are missing
 +or where the <tt>default</tt> keyword appears
  (7.6.1.3).
 @@ p2, Example 1
  The declaration
  
 - void point(int = 3, int = 4);
 + void point(int, int = 4);
  
  declares a function that can be called with
 -one, two, or three
 +one or two
  arguments of type int.
  It can be called in any of these ways:
  
 -  point(1,2);  point(1);  point();
 +  point(1,2);  point(1);  point(1,default);
  
 -The last two calls are equivalent to
 -<tt>point(1,4)</tt> and <tt>point(3,4)</tt>, respectively.
 +The last call is equivalent to
 +<tt>point(1,4)</tt>.
 @@ p2+1, Example 1+1
 +The declaration
 +
 + void point(int = 3, int);
 +
 +declares a function that can be called with
 +two arguments of type int.
 +It can be called in any of these ways:
 +
 + point(1,2);  point(default,2);
 +
 +The last call is equivalent to
 +<tt>point(3,2)</tt>.
 @@ p4
  For non-template functions,
  default arguments can be added
  in later declarations of a function
  that have the same host scope.
  Declarations that have different host scopes
  have completely distinct sets of default arguments.
  That is,
  declarations in inner scopes
  do not acquire default arguments
  from declarations in outer scopes,
  and vice versa.
 -In a given function declaration,
 -each parameter subsequent to a parameter with a default argument
 -shall have a default argument
 -supplied in this or a previous declaration,
 -unless the parameter was expanded from a parameter pack,
 -or shall be a function parameter pack.
 @@ p4, Example 2
  void g(int = 0, ...); // OK, ellipsis is not a parameter so it can follow
    // a parameter with a default argument
  void f(int, int);
  void f(int, int = 7);
  void h() {
  f(3);   // OK, calls f(3, 7)
 - void f(int = 1, int); // error: does not use default from surrounding scope
 + void f(int = 1, int); // does not use default from surrounding scope
 + f(default, 7);  // OK, calls f(1, 7)
 + f(3);   // error: wrong number of arguments
  }
  void m() {
  void f(int, int); // has no defaults
  f(4);   // error: wrong number of arguments
  void f(int, int = 5); // OK
  f(4);   // OK, calls f(4, 5);
  void f(int, int = 5); // error: cannot redefine, even to same value
  }
  void n() {
  f(6);   // OK, calls f(6, 7)
  }
-- 
<https://www.alejandro-colomar.es>

Received on 2026-12-02 21:17:17