[comp.lang.c++] scope of friend identifiers

daniel@terra.ucsc.edu (Daniel Edelson) (08/04/90)

The rules governing names introduced in friend
declarations appear somewhat ambiguous according
to [E&S 90], section 11.4. It says 

	``If a class or function mentioned as a 
	friend has not been declared its name is 
	entered in the same scope as the name of 
	the class containing the declaration.''


Does this imply that a friend declaration in a nested
class declares a member function of the enclosing class?
E.g.,

	struct out {
		struct in {
			friend void frfunc();
		};
	};

According to the rule, the name frfunc is scoped to out.
Therefore, must I use out::frfunc to define the function?

	void out::frfunc() { /* ? */ }

Have I now declared a member function of out without using the
standard syntax? Or alternatively, is frfunc NOT a member of out,
but rather a function whose name is simply in the scope of out,
with scope and membership being somewhat orthogonal.

Or, on the contrary, am I interpreting the rule incorrectly.
Does it simple mean that the friend function is in the scope of out
in the following respect?

	struct out {
		static int outstat;
		struct in {
			friend void frfunc();
		};
	};

void frfunc()
{
	int i = outstat;  /* Am I in the scope of out? */
}

Or both?

Similar semantic ambiguities apply to class names first appearing
in friend declarations.

	struct out {
		struct in {
			friend struct frstruct;
		};
	};

Is struct frstruct a nested type of struct out?

Then again, in section 3.2 the discussion of class scope says that
``A name first declared by a friend declaration belongs to the 
global scope;'' which, if true, contradicts the 11.4 statement and
renders this discussion moot. Is there a diffinitive answer as to
which section is correct?

Daniel Edelson
daniel@cis.ucsc.edu

rfg@NCD.COM (Ron Guilmette) (08/05/90)

In article <5708@darkstar.ucsc.edu> daniel@terra.ucsc.edu (Daniel Edelson) writes:
>The rules governing names introduced in friend
>declarations appear somewhat ambiguous according
>to [E&S 90], section 11.4. It says 
>
>	``If a class or function mentioned as a 
>	friend has not been declared its name is 
>	entered in the same scope as the name of 
>	the class containing the declaration.''
>
>
>Does this imply that a friend declaration in a nested
>class declares a member function of the enclosing class?
>E.g.,

This is an excellent question.  This is just the kind of question that ought
to be posted directly to comp.std.c++ (where it may get a more definitive
response).

>Then again, in section 3.2 the discussion of class scope says that
>``A name first declared by a friend declaration belongs to the 
>global scope;'' which, if true, contradicts the 11.4 statement and
>renders this discussion moot. Is there a diffinitive answer as to
>which section is correct?

If you find "bugs" (e.g. contradictions) in the C++ Reference Manual,
this should be noted over in comp.std.c++.  That will increase the
probability that such problems will come to the attention of the folks
who are currently drafting the C++ standard.

i
n
e
w
s

s
u
c
k
s

r
o
y
a
l

-- 
// Ron Guilmette
// C++ Entomologist
// Internet: rfg@ncd.com      uucp: ...uunet!lupine!rfg
// Motto:  If it sticks, force it.  If it breaks, it needed replacing anyway.

sakkinen@tukki.jyu.fi (Markku Sakkinen) (08/07/90)

Note2: Third trial to post!
Note1: This is the second posting of an article that apparently
disappeared while the news software was out of order here
for three days. Sorry if somebody receives it twice.

In article <5708@darkstar.ucsc.edu> daniel@terra.ucsc.edu (Daniel Edelson) writes:
>The rules governing names introduced in friend
>declarations appear somewhat ambiguous according
>to [E&S 90], section 11.4. It says 
>
>	``If a class or function mentioned as a 
>	friend has not been declared its name is 
>	entered in the same scope as the name of 
>	the class containing the declaration.''
>
>
>Does this imply that a friend declaration in a nested
>class declares a member function of the enclosing class?
>E.g.,
>
>	struct out {
>		struct in {
>			friend void frfunc();
>		};
>	};
>
>According to the rule, the name frfunc is scoped to out.

"Nested classes" in C++ are a pure deception; they are _not_
in the scope of the enclosing class. Thus, the definition
of 'in' is actually in an outer scope, and 'frfunc' belongs
to the same scope, without the least ambiguity.

> [...]

The above should answer also most of the rest of the questions
in the original posting, or make them irrelevant.

>Then again, in section 3.2 the discussion of class scope says that
>``A name first declared by a friend declaration belongs to the 
>global scope;'' which, if true, contradicts the 11.4 statement and
>renders this discussion moot. Is there a diffinitive answer as to
>which section is correct?

My version of the 2.0 Ref. Man. (from sometime in 1989) says in #3.2:
"... its name belongs to the enclosing scope; the same is true
for a name declared by a friend declaration (#11.4)."
There is thus a no contradiction in _that_ version,
but if yours is newer, it makes one wonder.

Markku Sakkinen
Department of Computer Science
University of Jyvaskyla (a's with umlauts)
Seminaarinkatu 15
SF-40100 Jyvaskyla (umlauts again)
Finland
          SAKKINEN@FINJYU.bitnet (alternative network address)

dwwx@cbnewsk.att.com (david.w.wood) (08/21/90)

I believe this is the case (Andrew Koenig will correct me if I'm wrong :-)

While it was true that nested class declarations were treated as being
declared with global scope, C++ 2.1 now supports nested scoping
so 
   class out {
	
	class in {
	}
   }

only out knows about in (helps keep the name space clean of supporting
classes) I have to wonder, though, how much code will break because
of this..... (Actually, I wish this had been supported since the
beginning, but better late than never...)

david wood