C++ Logo


Advanced search

C++ 20, Unqualified name look: a confusing relative to postfix expressions.

From: Vladimir Grigoriev <vlad.moscow_at_[hidden]>
Date: Wed, 04 Sep 2019 14:37:10 +0300
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 );
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.
With best regards, Vlad from Moscow

You can meet me at http://cpp.forum24.ru/ or www.stackoverflow.com or http://ru.stackoverflow.com

Received on 2019-09-04 06:39:17