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

suyog sarda sardask01 at gmail.com
Fri Mar 14 04:36:54 PDT 2014


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.


On Wed, Mar 12, 2014 at 5:24 AM, John McCall <rjmccall at apple.com> wrote:

> On Mar 10, 2014, at 9:07 AM, suyog sarda <sardask01 at gmail.com> wrote:
>
> This is regarding g++ test case
> g++.old-deja/g++.pt/friend10.C
>
> *template <class T>
> void f(T);
>
> template <class U>
> class C
> {
>   template <class T>
>   friend void f(T)
>   {
>      C<U> c;
>      c.i = 3;
>   }
>
>   public :
>
>      void g()
>      {
>        f(3.0);
>      }
>     int i;
> };
>
> int main ()
> {
>   f(7);
>   C<double> c;
>   c.g();
> }*
>
>
> Here, even when the body of function *f()* is defined, compiler throws
> error :
>
>
>
>
>
> *In function `main':19095.C:(.text+0x16): undefined reference to `void
> f<int>(int)' /tmp/19095-fd27b4.o: In function
> `C<double>::g()':19095.C:(.text._ZN1CIdE1gEv[_ZN1CIdE1gEv]+0x1b): undefined
> reference to `void f<double>(double)'clang-3.5: error: linker command
> failed with exit code 1*
>
> I have filed a bug related to this llvm.org/bugs/show_bug.cgi?id=19095
>
> g++ compiles this without any error.
>
> Please help in verifying if above is real bug. In my view this is a bug
> since function body has been defined and as it is declared friend it is a
> non-member function of class template.
>
>
> It does look like a bug.  Note that you can only instantiate this class
> template once, since the second instantiation would introduce a
> redefinition of f.
>
> John.
>



-- 
With regards,
Suyog Sarda
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140314/92a2c891/attachment.html>


More information about the cfe-dev mailing list