In section 11.5.1 of "The C++ Programming Language", Bjarne Stroustrup writes:
Like a member declaration, a friend declaration does not introduce a name into an enclosing scope.
For example:
class Matrix { friend class Xform; friend Matrix invert (const Matrix &); //.. }; Xform x; // error: no Xform in scope Matrix (*p) (const Matrix &) = &invert; // error: no invert() in scope
For large programs and large classes, it is nice that a class doesn’t ‘‘quietly’’ add names to its enclosing scope. For a template class that can be instantiated in many different contexts (Chapter 13), this is very important.
However, the next section then goes on to say that the class must have been previously defined, or defined in the non-class scope immediately enclosing the class that is declaring it a friend.
My question is that because of the fact that the class needs to be previous defined or defined in the nonclass scope immediately enclosing the class that is declaring it a friend, then in the first example Xform
could not be out of scope, as presumably the class would have been defined before the definition of the Matrix
class. Furthermore, I can't think of a situation which, given the restriction that the friend class needs to be previously defined or defined immediately after the granter's class, that the friend class will not be in scope!
Secondly, is my interpretation of Bjarne in this section correct, in that:
- For friend CLASSES only, friend class must have been previously defined in an enclosing scope, OR defined immediately after the non-class scope.
- For a function, must have been previously declared in an enclosing scope, OR it can also be found by having an argument of type == 'the friendship granter's' class?
Thanks
Taras