Topic: Access by friend function's default argument


Author: cludwig@informatik.uni-tuebingen.de (Christoph Ludwig)
Date: Thu, 1 Aug 2002 15:09:14 GMT
Raw View
Hi,

I posted this question some time ago to comp.lang.c++.moderated, but
there were no replies. Perhaps my question is more appropriate for
comp.std.c++, so I try it here.

I recently came across code similar to the following:

  #include <iostream>

  class A {
  private:
      static const int degree;

      friend void f(int);
  };

  const int A::degree = 5;    // line 10

  void f(int d = A::degree) { // line 12
      std::cout << "f(" << d << ") called" << std::endl;
  }

  int main() {
      f();
  }

Comeau C/C++ 4.2.45.2 (Linux) as well as the online preview of 4.3
compile above program without any complaint, even in strict
warnings mode.

Neither g++ 2.95.3 nor g++ 3.1 accept the program. They give the
following error message:

  defaultArgAccess.cc:10: `const int A::degree' is private
  defaultArgAccess.cc:12: within this context

It seems that Comeau's compiler considers the default argument
expression as part of f while g++ does not. BTW, it does not
matter whether the default argument appears in f's definition or in
a separate declaration.

I had a look into the respective standard clauses (8.3.6: Default
arguments, 11.4: Friends), but I was unable to decide which compiler
is correct. Can someone more knowledgeable clarifiy this matter
for me?

Regards

Christoph

---
[ 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                       ]