Friend function is not visible in the class

Abyx

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?

Mike Seymour

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

std::function as a friend of the class

From Dev

Making member function, friend of a class

From Dev

Private function as friend of other class

From Dev

Friend function of a private inner class

From Dev

Friend function from a templated class

From Dev

Friend function of a private inner class

From Dev

Function of one class friend of another class

From Dev

Member function of a class as friend to another class

From Dev

c++ class function friend to another class

From Dev

Defining a templated friend function inside a template class

From Dev

friend function for template class : C++

From Dev

Friend function is unable to construct a unique pointer of the class

From Dev

Linking error with friend function in template class

From Dev

declare template friend function of template class

From Dev

declare a C function as friend inside CPP class

From Dev

Templated Class Friend Operator Member Function

From Dev

Friend template function in-class definition

From Dev

Problem on declaring friend function with type traits in a class

From Dev

Linking error with friend function in template class

From Dev

Trying to call a friend function of template class

From Dev

Inline friend function defined in class body

From Dev

Can a virtual function access the friend of base class?

From Dev

gcc compiling error: member of nested class A in template class Table is not visible in nested friend class. Why?

From Dev

A friend function of a class that can only be used by a specific class

From Dev

How can I friend a derived class function in the base class?

From Dev

Friend function inside class and outside class, what difference does it make?

From Dev

Are friend functions inherited? and why would a base class FRIEND function work on a derived class object?

From Dev

Why can't a PRIVATE member function be a friend function of another class?

From Dev

Setting a value by a friend function which is a member function of another class

Related Related

  1. 1

    std::function as a friend of the class

  2. 2

    Making member function, friend of a class

  3. 3

    Private function as friend of other class

  4. 4

    Friend function of a private inner class

  5. 5

    Friend function from a templated class

  6. 6

    Friend function of a private inner class

  7. 7

    Function of one class friend of another class

  8. 8

    Member function of a class as friend to another class

  9. 9

    c++ class function friend to another class

  10. 10

    Defining a templated friend function inside a template class

  11. 11

    friend function for template class : C++

  12. 12

    Friend function is unable to construct a unique pointer of the class

  13. 13

    Linking error with friend function in template class

  14. 14

    declare template friend function of template class

  15. 15

    declare a C function as friend inside CPP class

  16. 16

    Templated Class Friend Operator Member Function

  17. 17

    Friend template function in-class definition

  18. 18

    Problem on declaring friend function with type traits in a class

  19. 19

    Linking error with friend function in template class

  20. 20

    Trying to call a friend function of template class

  21. 21

    Inline friend function defined in class body

  22. 22

    Can a virtual function access the friend of base class?

  23. 23

    gcc compiling error: member of nested class A in template class Table is not visible in nested friend class. Why?

  24. 24

    A friend function of a class that can only be used by a specific class

  25. 25

    How can I friend a derived class function in the base class?

  26. 26

    Friend function inside class and outside class, what difference does it make?

  27. 27

    Are friend functions inherited? and why would a base class FRIEND function work on a derived class object?

  28. 28

    Why can't a PRIVATE member function be a friend function of another class?

  29. 29

    Setting a value by a friend function which is a member function of another class

HotTag

Archive