Topic: default arg can see parameter names?


Author: "Scott McPeak" <garbaqq@yahoo.com>
Date: 1 Sep 2005 05:50:22 GMT
Raw View
Can a default argument expression see the parameters in the same
function declaration?  If so, can a default argument for parameter 'x'
see 'x' itself?

Examples:
  void f(int x = x);            // Q1: legal?
  void g(int x, int y = x);     // Q2: legal?
  struct A {
    void f(int x, int y = x);   // Q3: legal?
    void g(int x, int y = x){}  // Q4: legal?
  };

Granted, if case 1 is legal then the value of 'x' is indeterminate;
but that's just like (the legal) "int x = x;" case.

8.3.6p5: "names in the expression are bound ... at the point where
  the default argument expression appears.  ... Note: in member
function
  declarations, names in default argument expressions are looked up
  as described in 3.4.1"
3.4.1p11: "During the lookup for a name used as a default argument ...
  parameter names are visible and hide the names of entities declared
  in ... scopes containing the function declaration."
3.3.1p1: "The point of declaration for a name is immediately after its
  complete declarator (clause 8) and before its initializer (if any),
  except as noted below."  None of the subsequent paragraphs provides
  an exception for parameters and default arguments.

The above suggests that the parameter should be visible in the default
argument.  Other paras in 3.4.1 give scope search orders for various
contexts:
  3.4.1p5: outside any function or class
  3.4.1p6: outside any class, but in a function definition
  3.4.1p7: in a class, but outside any function definition
  3.4.1p8: in a member function definition
p6 and p8 say to search in the enclosing blocks, which implicitly
includes the parameters because they go in the outmost block of the
function.  So in those two cases, parameters should be visible in
default arguments.

3.4.1p7 does not mention searching in blocks, because none exist.
There is a function prototype scope in member function declarations
(that are not definitions), and it contains the parameters, but this
paragraph does not mention searching in the prototype scope.  So,
either this is contradictory with 3.4.1p11, or the latter intends to
"patch" the scope search order specified in 3.4.1p7.  This is unclear.

I did some experimenting with gcc-3.3.4, gcc-3.4.4 and gcc-2.95.3, and
all versions seem to *never* allow default argument expressions to
refer to parameters, even those occurring earlier.

-Scott

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: hyrosen@mail.com (Hyman Rosen)
Date: Fri, 2 Sep 2005 04:11:09 GMT
Raw View
Scott McPeak wrote:
> Can a default argument expression see the parameters in the same
> function declaration?

Yes, but it may not use them. You seemed to get as far as 8.3.6/5,
but you should have gone down a little more, to 8.3.6/9:
     "Parameters of a function declared before a default argument
      expression are in scope and can hide namespace and class member
      names."
and
     "...parameters of a function shall not be used in default
      argument expressions..."

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]