https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62244
Bug ID: 62244
Summary: Function parameter should be in scope in its own
default argument
Product: gcc
Version: 4.9.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: zeratul976 at hotmail dot com
Consider the following code:
int a;
void f(int a = a);
GCC accepts this code, and name lookup for the 'a' in the default argument
finds the global variable 'a' (as suggested by the fact that if the global
variable 'a' is commented out, GCC now gives "error: 'a' was not declared in
this scope").
I believe this in incorrect. During name lookup for the 'a' in the default
argument, the parameter 'a' should already be in scope, since
[basic.scope.pdecl]/p1 says that "The point of declaration for a name is
immediately after its complete declarator and before its initializer (if any)".
Once name lookup finds the parameter, the program should then be ill-formed,
because [dcl.fct.default]/p9 says "parameters of a function shall not be used
in a default argument".
Note that GCC has the correct behaviour when a later parameter's default
argument references an earlier parameter:
int a;
void f(int a, int b = a); // error: local variable 'a' may not appear in this
context
The incorrect behaviour only occurs when a parameter's own default argument
references the parameter.
Note that clang rejects the first example with "error: default argument
references parameter 'a'".