Title
Default arguments in list-initialization
Status
open
Section
9.5.5 [dcl.init.list]
Submitter
Brian Bi

Created on 2025-04-24.00:00:00 last changed 2 weeks ago

Messages

Date: 2025-04-24.00:00:00

Consider:

  #include <iostream>
  struct I {
    I(int x) { std::cout << x; }
  };
  struct S {
    S(I, I = 2) {}
  };
  int main() {
    S(1, 2); std::cout << '\n';  // unspecified order; prints 12 or 21
    S{1, 2}; std::cout << '\n';  // prints 12 (9.5.5 [dcl.init.list] paragraph 4)
    S{1};                        // ???
  }

Since no initializer-clause is present for the second argument, 9.5.5 [dcl.init.list] paragraph 4 does not, but should, prescribe lexical ordering of the argument evaluations.

Possible resolution:

Change in 9.5.5 [dcl.init.list] paragraph 4 as follows:

Within the initializer-list of a braced-init-list, the initializer-clauses, including any that result from pack expansions (13.7.4 [temp.variadic]), are evaluated in the order in which they appear. That is, every value computation and side effect associated with a given initializer-clause is sequenced before every value computation and side effect associated with any initializer-clause that follows it in the comma-separated list of the initializer-list. If the elements of the initializer-list are interpreted as arguments of a constructor call (12.2.2.8 [over.match.list]), any default arguments are evaluated in the order as-if an initializer-clause were present for the corresponding parameter.

[Note 4: This evaluation ordering holds regardless of the semantics of the initialization; for example, it applies when the elements of the initializer-list are interpreted as arguments of a constructor call, even though ordinarily there are no sequencing constraints on the arguments of a call. [ Example:

  #include <iostream>
  struct I {
    I(int x) { std::cout << x; }
  };
  struct S {
    S(I, I = 2) {}
  };
  int main() {
    S(1, 2); std::cout << '\n';  // unspecified order; prints 12 or 21
    S{1, 2}; std::cout << '\n';  // prints 12
    S{1};                        // prints 12
  }
-- end example ]end note]

History
Date User Action Args
2025-04-24 00:00:00admincreate