Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A friend function accessing a private member of a friend class

Following the Czech song from Eurovision 2019 in Tel-Aviv

It is known that in C++ a friend of a friend is not (automatically) a friend.

Clang however differs on the following code with GCC and MSVC:

class A {
public:    
    // forward declaration
    class Inner2;

private:
    class Inner1 {
        char foo;
        friend class Inner2;
    };
public:
    class Inner2 {
        Inner1 i;
    public:
        bool operator==(Inner2 other) {
            return i.foo == other.i.foo; // OK by GCC, Clang and MSVC++
        }
        friend bool operator!=(Inner2 a, Inner2 b) {
            return a.i.foo != b.i.foo; // Clang accepts, GCC and MSVC++ reject
        }
    };
};

Code: https://godbolt.org/z/rn48PTe1Y

Which one is correct? If Clang is wrong by being too permissive, what is the best way to allow access (other than providing a public getter?)


A note: if the friend function is just declared in the class and implemented outside, both Clang and GCC reject the code.

like image 903
Amir Kirsh Avatar asked May 28 '21 15:05

Amir Kirsh


People also ask

Can a friend function access private members of a class?

A friend function cannot access the private and protected data members of the class directly. It needs to make use of a class object and then access the members using the dot operator. A friend function can be a global function or a member of another class.

How do I access private member Friends class?

Friend Keyword in C++ You can use the friend keyword to any class to declare it as a friend class. This keyword enables any class to access private and protected members of other classes and functions.

Can friend function manipulate private members?

Yes, In principle, private and protected members of a class cannot be accessed from outside the same class in which they are declared. However, this rule does not affect friends. Friends are functions or classes declared with the friend keyword.

Can a non-member function access the private members of a class?

Generally, non-member functions cannot access the private members of a particular class. Once declared as a friend function, the function is able to access the private and the protected members of these classes. class class_name { friend data_type function_name (arguments/s); //syntax of friend function.

What is friend class and function in C++?

Friend class and function in C++. Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful to allow a particular class to access private members of other class.

Is it possible to access private members outside a class without friend?

The idea of Encapsulation is to bundle data and methods (that work on the data) together and restrict access of private data members outside the class. In C++, a friend function or friend class can also access private data members. So, is it possible to access private members outside a class without friend?

Are friend functions member functions?

Even though the prototypes for friend functions appear in the class definition, friends are not members functions. We can declare friend functions anywhere in a class definition, that is either in public, private or protected sections.


1 Answers

It seems to be a known defect in clang, bug id #11515, reported already in 2011, but still not fixed, apparently.

An even simpler example that compiles, and shouldn't (from the bug report above):

class A {
  int n;
  friend struct B;
};

struct B {
  friend int get(A &a) {
    return a.n; // clang accepts, and should reject
  }
};

https://godbolt.org/z/r78Pazoqj

like image 188
Amir Kirsh Avatar answered Oct 19 '22 20:10

Amir Kirsh