C++ Logo


Advanced search

Subject: Re: C++ 20, Unqualified name look: a confusing relative to postfix expressions.
From: Brian Bi (bbi5291_at_[hidden])
Date: 2019-09-04 09:42:03

On Wed, Sep 4, 2019 at 6:37 AM Vladimir Grigoriev via Std-Discussion <
std-discussion_at_[hidden]> wrote:

> According to the C++ 20 Standard(11.8.3 Friends)
> 7 Such a function is implicitly an inline function (9.1.6). *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
> (6.4.1).
> Now let's consider the following example
> #include <iostream>
> typedef int f;
> namespace N
> {
> struct A
> {
> friend void f( A &a ) { std::cout << "F( " << a.i << " )\n"; }
> operator int() const { return i; }
> static void g( A &a )
> {
> f( a );
> }
> private:
> int i = 10;
> };
> };
> int main()
> {
> N::A a;
> N::A::g( a );
> }
> As you see the friend function f is defined withing the class A. So it is
> in the lexical scope of the class. However in the expression statement of
> the static member function g
> f( a );
> the compiler considers this statement as a variable declaration with the
> type specifier int according to the typedef defined outside the namespace N.
> I did not find a corresponding description in the C++ Standard that would
> confirm that such a behavior of the compiler is correct. It seems that if
> the friend function is in the lexical scope of the class A its declaration
> should hide the definition of the typedef outside the namespace N.
> MS VC++ considers the above statement as a postfix expression of the
> friend function call. However gcc and clang behave differently even if the
> expression to enclose in parentheses..
> So I would like to know the corresponding paragraphs from the Standard
> that shed light on this situation.
I think the standard is saying that unqualified name lookup inside the
definition of the friend function will find class members, whereas if the
friend function's definition is outside the class, this would not happen.
This is the only interpretation that seems to make sense to me.

The quoted paragraph therefore appears to be redundant with
[basic.lookup.unqual]/9 <http://eel.is/c++draft/basic.lookup.unqual#9>:

Name lookup for a name used in the definition of a friend function defined
> inline in the class granting friendship shall proceed as described for
> lookup in member function definitions. If the friend function is not
> defined in the class granting friendship, name lookup in the friend
> function definition shall proceed as described for lookup in namespace
> member function definitions.

It would thus seem that [class.friend]/7 should be deleted or should be
changed to a note (possibly reworded).

> With best regards,
> Vlad from Moscow
> You can meet me at http://cpp.forum24.ru/ or www.stackoverflow.com or
> http://ru.stackoverflow.com
> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion

*Brian Bi*

STD-DISCUSSION list run by herb.sutter at gmail.com

Older Archives on Google Groups