Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Friend function is not visible in the class

Tags:

c++

I have the following code:

struct M {
    friend void f() {}
    M() {
        f(); // error: 'f' was not declared in this scope
    }
};

int main() {
    M m;
}

Live example

Both g++4.8 and clang3.4 fail to compile it, because f is not visible inside M, or so they say.

However, the Standard gives an example of a similar code

class M {
  friend void f() { } // definition of global f, a friend of M,
                      // not the definition of a member function
};

and says that

A friend function defined in a class is in the (lexical) scope of the class in which it is defined.

(ISO/IEC 14882:2011 11.3 Friends [class.friend] p6, p7)

From this I can't understand how compiler can't find f which is defined in same class where it's used.

It's kinda unlikely that both compilers have the same bug.
So, what did I miss?

like image 334
Abyx Avatar asked May 08 '14 11:05

Abyx


People also ask

Can friend function be defined in class?

Friend functions can be defined (given a function body) inside class declarations. These functions are inline functions. Like member inline functions, they behave as though they were defined immediately after all class members have been seen, but before the class scope is closed (at the end of the class declaration).

Is friend function private or public?

A friend function can be declared in the private or public section of the class. It can be called like a normal function without using the object. A friend function is not in the scope of the class, of which it is a friend. A friend function is not invoked using the class object as it is not in the scope of the class.

Can we declare friend function outside the class?

The friend function can be a member of another class or a function that is outside the scope of the class. A friend function can be declared in the private or public part of a class without changing its meaning. Friend functions are not called using objects of the class because they are not within the class's scope.

Why There Is No friend function in Java?

Java does not have the friend keyword from C++. There is, however, a way to emulate that; a way that actually gives a lot more precise control. Suppose that you have classes A and B. B needs access to some private method or field in A.


1 Answers

The friend declaration states that a function called f in the surrounding namespace is a friend of the class; but it does not introduce the name f into the namespace. It's not available (except by argument-dependent lookup) until it's been declared in the namespace.

The relevant rule is C++11 7.3.1.2/3:

If a friend declaration in a non-local class first declares a class or function the friend class or function is a member of the innermost enclosing namespace. The name of the friend is not found by unqualified lookup or by qualified lookup until a matching declaration is provided in that namespace scope.

like image 102
Mike Seymour Avatar answered Oct 25 '22 01:10

Mike Seymour