The following code received no diagnostic in 4.0.1, but on 4.2.1 gets: ~/ootbc/personal/ivan$ g++ foo.cc foo.cc: In instantiation of 'foo2<int>': foo.cc:16: instantiated from here foo.cc:12: error: redefinition of 'void bar(int)' foo.cc:5: error: 'void bar(int)' previously defined here
Test case: template<typename T> class foo1 { public: friend void bar(T t) {}; }; template<typename T> class foo2 { public: friend void bar(T t) {}; }; foo1<int> f1; foo2<int> f2; int main() {} This may just be a result of tightening up gcc to the standard and the code always was wrong, but I submit the report in case it is actually a regression in a rarely-used part of the language. As I understand it, every function defined in a template is considered to itself be a template, and hence having a template function that is also a definition does not count as a duplicate definition no matter how many times the template is instantiated. However, that seems to no longer apply for friends declared and defined in templates. Note that both friends refer to the same function, which is textually declared identically. Incidentally, declaring the friends "inline" makes no difference to the diagnostic, whereas it would for a plain function. Possibly the construction does in fact reflect the standard, but the inline keyword is mishandled? I leave to the language lawyers whether this is valid or a regression, but in any case you might want to add this code to your regression suite. -- Summary: frend functions of template get new diagnostic Product: gcc Version: 4.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: igodard at pacbell dot net http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33150