c++ - Weird behavior with operator defined as friend inside class -
i don't understand going on in following piece of code:
struct { }; struct b { b() { } b(const a&) { } friend b operator*(const b&, const b&) { return b(); } }; int main() { b x = a() * a(); return 0; }
when compile (with both clang , gcc 4.9.2) error message on "b x = a() * a()" line; clang says "invalid operands binary expression".
if take operator* definition inside class, 100% ok!
struct { }; struct b { b() { } b(const a&) { } friend b operator*(const b&, const b&); }; b operator*(const b&, const b&) { return b(); } int main() { b x = a() * a(); return 0; }
what going on?
since operator*()
defined inside function friend, can found adl, not normal unqualified lookup. type of lookup requires arguments exact types, not types implicitly convertible. means operator cannot found if a
can converted b
.
when declare function outside class, can found normal unqualified lookup rules.
Comments
Post a Comment