[cfe-dev] [Bug] Undefined reference to function template declared friend and defined in template class
suyog sarda
sardask01 at gmail.com
Sat Mar 15 02:48:56 PDT 2014
I observed following points by changing the test case as per above
discussion :
1. If i move the function template definition outside the class template,
the compiler doesn't complain about not finding the definition and compiles
the code without any error. This is normal behavior and the bug was not
related to this case, as the compiler finds the function definition.
2. If i change the friend function template (defined inside the class) into
non-template friend function with definition inside the class template, the
compiler complains of redefinition of the function :
*' error: redefinition of 'f' friend void f(int r){
^19095.cpp:28:13: note: in instantiation of template class 'C<double>'
requested here C<double> c;' *
3. If the test case is unchanged, then compiler complains of not finding
the body.
4. Agreed to above point that when class template will be instantiated
twice, it will result in redefinition of the friend function.
I agree that with respect to use case, its always good to move definition
outside the class. However, as it is legal to define friend function inside
class, hence the above bug.
I have sent a patch related to this test case to cfe-commits under the
subject
'[PATCH] PR19095 Undefined reference to friend template function defined
inside template class'
which resolves the 'undefined reference' error. I haven't included test
case yet in the patch. Please help in reviewing the same.
On Fri, Mar 14, 2014 at 11:45 PM, John McCall <rjmccall at apple.com> wrote:
> 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.
--
With regards,
Suyog Sarda
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140315/67cf85ec/attachment.html>
More information about the cfe-dev
mailing list