Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to take address of an overloaded operator defined as a friend in class body in C++

How can I take an address of an overloaded operator defined as a friend in a class body?

I tried the following

struct S {
  friend bool operator==(S, S) { return true; }
};

int main() {
  bool (*f)(S, S) = &operator==;
}

But gcc gives an error

test.cc: In function ‘int main()’:
test.cc:6:30: error: ‘operator==’ not defined
   bool (*f)(S, S) = &operator==;
                              ^

which can be fixed by declaring the operator in the (global) namespace:

bool operator==(S, S);

Is there a way to take the address without redeclaring the operator?

like image 897
vitaut Avatar asked Mar 14 '23 09:03

vitaut


2 Answers

A name first declared in a friend declaration within class or class template X becomes a member of the innermost enclosing namespace of X, but is not accessible for lookup (except argument-dependent lookup that considers X) unless a matching declaration at the namespace scope is provided

http://en.cppreference.com/w/cpp/language/friend, emphasis mine.

Generally, you should always declare your friends also outside of a class, unless you explicitly want to hide them from any lookup except ADL (which might be good idea and might be not, I don't have enough experience to judge, and this really depends on a situation.).

like image 99
Petr Avatar answered Apr 30 '23 17:04

Petr


Is there a way to take the address without redeclaring the operator?

No, the name is in the scope of class, you can't use it directly.

From the standard, 11.3$6,7 Friends [class.friend] (bold by me)

6 A function can be defined in a friend declaration of a class if and only if the class is a non-local class (9.8), the function name is unqualified, and the function has namespace scope.

7 Such a function is implicitly inline. 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 (3.4.1).

like image 39
songyuanyao Avatar answered Apr 30 '23 15:04

songyuanyao