Jens Maurer
2010-11-12
N3217=10-0207

Wording for brace-initializers as default arguments

This paper presents detailed wording changes relative to the current C++ Working Draft N3126 to implement brace-initializers for default arguments for functions, as proposed in N3139 "An Incomplete Language Feature" by Bjarne Stroustrup, thereby also addressing core issue 994.

Change the grammar in 8.3.5 dcl.fct paragraph 3:


parameter-declaration:
      attribute-specifieropt decl-specifier-seq declarator
      attribute-specifieropt decl-specifier-seq declarator = initializer-clause assignment-expression
      attribute-specifieropt decl-specifier-seq abstract-declaratoropt
      attribute-specifieropt decl-specifier-seq abstract-declaratoropt = initializer-clause assignment-expression
Change in 8.3.6 dcl.fct.default paragraph 1:
If an expression initializer-clause is specified in a parameter declaration parameter-declaration this expression initializer-clause is used as a default argument. Default arguments will be used in calls where trailing arguments are missing.
Change in 8.3.6 dcl.fct.default paragraph 3:
A default argument expression shall be specified only in the parameter-declaration-clause of a function declaration or in a template-parameter (14.1 temp.param); in the latter case, the initializer-clause shall be an assignment-expression. It A default argument shall not be specified for a parameter pack. ...
Change in 8.3.6 dcl.fct.default paragraph 5:
A default argument expression is implicitly converted (Clause 4 conv) to the parameter type. The default argument expression has the same semantic constraints as the initializer expression in a declaration of a variable of the parameter type, using the copy-initialization semantics (8.5 dcl.init). The names in the default argument expression are bound, and the semantic constraints are checked, at the point where the default argument expression appears. Name lookup and checking of semantic constraints for default arguments in function templates and in member functions of class templates are performed as described in 14.7.1 temp.inst. [ Example: ... ] [ Note: in member function declarations, names in default arguments argument expressions are looked up as described in 3.4.1 basic.lookup.unqual. Access checking applies to names in default arguments argument expressions as described in Clause 11 class.access. -- end note ]
Change in 8.3.6 dcl.fct.default paragraph 7:
Local variables shall not be used in a default argument expressions. [ Example: ... ]
Change in 8.3.6 dcl.fct.default paragraph 9:
... Consequently, parameters of a function shall not be used in a default argument expressions, even if they are not evaluated. Parameters of a function declared before a default argument expression are in scope and can hide namespace and class member names. [ Example: ... ] Similarly, a non-static member shall not be used in a default argumentexpression, even if it is not evaluated, unless it appears as the id-expression of a class member access expression (5.2.5 expr.ref) or unless it is used to form a pointer to member (5.3.1 expr.unary.op). [ Example: ... ]
Change 1.9 intro.execution paragraph 11:
[ Note: the evaluation of a full-expression can include the evaluation of subexpressions that are not lexically part of the full-expression. For example, subexpressions involved in evaluating default arguments argument expressions (8.3.6 dcl.fct.default) are considered to be created in the expression that calls the function, not the expression that defines the default argument. -- end note ]
Change in 3.4.1 basic.lookup.unqual paragraph 6:
A name used in the definition of a function following the function's declarator-id [ Footnote: This refers to unqualified names that occur, for instance, in a type or default argument expression in the parameter-declaration-clause or used in the function body. ] that is a member of namespace N (where, only for the purpose of exposition, N could represent the global scope) shall be declared before its use in the block in which it is used or in one of its enclosing blocks (6.3 stmt.block) or, shall be declared before its use in namespace N or, if N is a nested namespace, shall be declared before its use in one of N's enclosing namespaces. [ Example: ... ]
Change in 3.4.1 basic.lookup.unqual paragraph 8:
A name used in the definition of a member function (9.3 class.mfct) of class X following the function's declarator-id [ Footnote: That is, an unqualified name that occurs, for instance, in a type or default argument expression in the parameter-declaration-clause or in the function body. ] or in the brace-or-equal-initializer of a non-static data member (9.2 class.mem) of class X shall be declared in one of the following ways: ...
Change in 7.1.2 dcl.fct.spec paragraph 4:
... [ Note: A string literal appearing in a default argument expression is not in the body of an inline function merely because the expression is used in a function call from that inline function. -- end note ] ...
Change in 8.5 dcl.init paragraph 3:
[ Note: default arguments argument expressions are more restricted; see 8.3.6 dcl.fct.default.
Change in 11 class.access paragraph 8:
The names in a default argument expression (8.3.6 dcl.fct.default) are bound at the point of declaration, and access is checked at that point rather than at any points of use of the default argument expression. ...
Change in 12.2 class.temporary paragraph 4:
If the constructor has one or more default arguments, the destruction of every temporary created in a default argument expression is sequenced before the construction of the next array element, if any.

Change in 14.7.1 temp.inst paragraph 11:

If a function template f is called in a way that requires a default argument expression to be used, the dependent names are looked up, the semantics constraints are checked, and the instantiation of any template used in the default argument expression is done as if the default argument expression had been an expression initializer used in a function template specialization with the same scope, the same template parameters and the same access as that of the function template f used at that point. ...