Re: New -ffriend-injection behavior - is it really intended?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Daniel Lohmann <daniel.lohmann@xxxxxxxxxxxxxxxxxxxxxxxxxx> writes:

> Ian Lance Taylor schrieb:
> >    "A function can be defined in a friend declaration of a class if
> >     and only if the class is a non-local class (9.8), the function
> >     name is unqualified, and the function has namespace scope.  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)."
> > There is nothing there which makes the friend function visible to any
> > other use.  You need an explicit declaration outside of the class to
> > permit other classes to see the function.
> 
> Yes, this is exaclty the part I mean.
> However, I do not understand how to come to your conclusion:
> 
> 1) I guess the relevant sentence is: "A friend function defined in a
> class is in the (lexical) scope of the class in which *it* is
> defined." The question is what exactly the *it* refers to. To the
> class or to the function? What makes you sure it refers to the
> function?
> (I should mention that I am not a native speaker, so this might be a
> simple language issue.)

"it" refers to the function.  I agree this is ambiguous, but it would
be contorted English for "it" to refer to the class.  And it would not
make sense if the class were not itself defined in some other class.


> 2) In my standard, there is also an illustrating example:
> [--example]
> class C {
>    friend void f() {}
> };
> [--end example]
> 
> The point is, that with the g++ 4.1 interpretation of the standard,
> f() is not visible in *any* scope:
> 
> lohmann@faui48a [~]>cat t.cc
> class C {
>    friend void f() {}
> 
>    void g() { f(); }
> };
> 
> int main() {
>    f();
>   }
> 
> lohmann@faui48a [~]>g++ t.cc
> t.cc: In member function 'void C::g()':
> t.cc:4: error: 'f' was not declared in this scope
> t.cc: In function 'int main()':
> t.cc:8: error: 'f' was not declared in this scope
> lohmann@faui48a [~]>
> 
> If g++ 4.1 interprets the standard in the way that f() is in the
> lexical scope spanned by class C it should be found by g(). If (as
> older versions did) the standard is interpreted in the way that f() is
> the scope in which C is defined, it should be found by main(). But
> neither scope? If this is really correct, it looks like a language
> defect to me.

Hmmm.  That does look strange.  I would have expected that f would be
found in C::g, but I agree that gcc does not work that way at present.
This may be a gcc bug.

I'm confident that it's not a gcc bug that f is not found in main.


> 3) How do you read from the standard that a friend function defined in
> a class *can* be found from the enclosing namespace (only) by argument
> dependent lookup (as stated in the -ffriend-injection documentation)?
> 
> 4) And finally, do you know the *intention* behind all this? I am
> trying to imagine a good reason for the g++ 4.1 interpretation, but
> haven't been able to find one so far. I mean, it is just not a strong
> point for some interpretation if it obviously does not make sense...

I don't know.

I recommend asking on a C++ language mailing list.

Ian

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux