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

可紊 提交于 2019-12-23 14:53:51

问题


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?


回答1:


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




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



来源:https://stackoverflow.com/questions/33283092/how-to-take-address-of-an-overloaded-operator-defined-as-a-friend-in-class-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!