15625 – [3.4/4.0 Regression] friend defined inside a template fails to find static function (original) (raw)

| Description Ivan Godard 2004-05-24 01:34:52 UTC enum Relation {equalOp}; template class A { public: static bool Relop(const A&, const A&, Relation); friend bool operator==(const A& a1, const A& a2) { return Relop(a1, a2, equalOp); } B* b; }; int main() { A a; a == a; return 0; } produces: foo.cc: In function `bool operator==(const A&, const A&)': foo.cc:16: instantiated from here foo.cc:10: error: no matching function for call to `A::Relop(const A&, const A&, Relation)' foo.cc:6: note: candidates are: static bool A::Relop(const A&, const A&, Relation) Even if this code is actually invalid (who could tell except the committee?) the error message should at least indicate what is really wrong. Ivan Comment 1 Drea Pinski 2004-05-24 02:38:52 UTC Confirmed, a regression, this is template related. Comment 2 Wolfgang Bangerth 2004-05-24 13:57:13 UTC Here is something slightly smaller: ------------------- template struct S { static void foo(const S&); friend void bar(const S& s) { foo(s); } }; int main() { S a; bar(a); } ------------------ Although all prior versions of gcc before 3.4 and icc in strict mode accept this code, I am a little puzzled why this is so: bar() is just a friend, not a member, why should it look inside struct S to find the static function? Why don't I have to qualify foo as S::foo? Alas, that isn't the problem, from the error message gcc gives, it is pretty obvious that gcc just gets somehow confused about templates in this case. W. Comment 3 Mark Mitchell 2004-05-31 18:32:30 UTC Working on a fix. Comment 4 Mark Mitchell 2004-05-31 18:34:39 UTC This code is invalid; according to the standard, the friend function should not be injected into the namespace scope. (This is a change from the ARM.) However, g++ has always done the injection, and we have not yet removed that functionality, so this code should work as expected. The standard-conforming way of doing this is to have a namespace-scope declaration of the function. You can still define it in the class, but it should be declared outside the class. This comment is meant to answer Wolfgang's question regarding this seems to work at all. Comment 7 Mark Mitchell 2004-05-31 21:29:20 UTC Fixed in GCC 3.4.1. | | | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |