4

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?

vitaut
  • 49,672
  • 25
  • 199
  • 336

2 Answers2

5

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.).

Petr
  • 9,812
  • 1
  • 28
  • 52
2

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).

songyuanyao
  • 169,198
  • 16
  • 310
  • 405