[cfe-dev] [Bug] Undefined reference to function template declared friend and defined in template class

John McCall rjmccall at apple.com
Fri Mar 14 11:15:45 PDT 2014


On Mar 14, 2014, at 4:36 AM, suyog sarda <sardask01 at gmail.com> wrote:
> Will the second instantiation of the class template introduce redefinition of f? In my opinion, friend functions are 
> non-member functions of class (independent of the class). So, no matter how many times we instantiate the class template, 
> there won't be redefinition of the function. Please correct me if i am wrong.

You are correct that a friend function is not a class member.  However, it is still templated, and instantiating a class effectively inserts redeclarations of its friend functions into the appropriate scopes.  When those redeclarations resolve to the same entity (i.e. when their signature does not depend on the enclosing template parameters), they can end up defining it multiple times.

Among other things, recall that the definition appears within a template and therefore its body can depend on those template parameters.

We should fix the bug that you’ve identified, but I highly recommend actually moving the function definition outside of the class.

John.



More information about the cfe-dev mailing list