On 10/20/2010 7:49 AM, moogyd wrote:
> Hi,
> I am a C++ newbie, and have aa C++ project that compiles with the -
> ffriend-injection (g++) option.
That option is compiler-specific. Consider asking in the g++ newsgroup.
Read on, however.
>
> I am trying to understand what this option actually does, so that I
> can fix the code. Unfortunately, google provides lots of descriptions
> based on templates. that are way above my level of understanding.
>
> I therefore created a very simple example:
>
> namespace myNamespace {
> class myClass {
> friend int compute(myClass arg) {} ;
> } ; // myClass
> // If compute is not declared here, then I need the g++
> // -ffriend-injection option
> int compute(myClass arg);
> } // myNamespace
>
> int main() {
> myNamespace::myClass myVar ;
> myNamespace::compute(myVar) ;
> return 0 ;
> }
> This compiles without -ffriend_injection. If I comment the
> declaration int compute(..). then the option is required.
> Without the namespece, it always compiles.
>
> Therefore, it seems to me that friend injection means that
> - all friend functions automatically become visible in the surrounding
> namespace (myNamespace in this example)
>
> Is this correct? Can anyone supply a (or a link to) a very simple
> explanation.
Here is a quote from the current Standard ([class.friend]/5):
"A function can be defined in a friend declaration of a class if and
only if the class is a non-local class (9.

, the function name is
unqualified, and the function has namespace scope. [Example:
class M {
friend void f() { } // definition of global f, a friend of M,
// not the definition of a member function
};
—end example] Such a function is implicitly inline. A friend function
defined in a class is in the (lexical) scope of the class in which it is
defined. A friend function defined outside the class is not (3.4.1)."
Note the wording about the scope. If the function is *in* the scope of
the class, it should be found during ADL since your function's argument
is the class that defines that function, but *ONLY* if the function name
is unqualified:
...
int main() {
myNamespace::myClass myVar;
compute(myVar); // unqualified name - found during ADL
}
Since you qualified that name, the ADL doesn't kick in, and the class
scope is not searched, only the namespace scope. Since the function
defined in the class is not *in* the namespace scope (it wasn't declared
there), it is not found.
Now, there are two statements about scope in that paragraph. The first
one says "the function *has* namespace scope" (emphasis mine), which to
me means that *inside* the function the name resolution works from the
namespace scope in which the *class* resides. IOW, if the function had
a name 'foo' in it, and your 'myNamespace' had a 'foo', that 'foo' would
be found.
I don't know *for sure* what your option does, ask in the g++ newsgroup,
but most likely it makes the *name* of the function *available* in the
same namespace as the class (as if it were declared there).
V
--
I do not respond to top-posted replies, please don't ask