How to take address of an overloaded operator defined as a friend in class body in C++ -
how can take address of overloaded operator defined friend in class body?
i tried following
struct s { friend bool operator==(s, s) { return true; } }; int main() { bool (*f)(s, s) = &operator==; }
but gcc gives error
test.cc: in function ‘int main()’: test.cc:6:30: error: ‘operator==’ not defined bool (*f)(s, s) = &operator==; ^
which can fixed declaring operator in (global) namespace:
bool operator==(s, s);
is there way take address without redeclaring operator?
a name first declared in friend declaration within class or class template x becomes member of innermost enclosing namespace of x, is not accessible lookup (except argument-dependent lookup considers x) unless matching declaration @ namespace scope provided
http://en.cppreference.com/w/cpp/language/friend, emphasis mine.
generally, should declare friends outside of class, unless explicitly want hide them lookup except adl (which might idea , might not, don't have enough experience judge, , depends on situation.).
Comments
Post a Comment