This is the mail archive of the egcs-bugs@egcs.cygnus.com mailing list for the EGCS project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Template Friend Weirdness


Guys,

I'm trying to declare a global template function to be a friend of a template
class.  Problems with this are leading in a mysterious and seemingly broken
way to a template f'n failing to be instantiated.  Here's the code:

---

#include <stdio.h>

template<class T> void TFunc(T& bob){
        printf("TFunc() says bob's i = %d\n",bob.i);
}

template<class T> class SomeClass {
public:
    SomeClass(){i=123456789;};
    ~SomeClass(){};

private:
        T i;

        friend void TFunc(SomeClass<T>&);
};

int main() {
        SomeClass<int> SC;
        TFunc(SC);
        return 0;
}

---

This compiles and runs fine on Microsoft C++, Borland C++ for Windows and
the SGI MIPSpro C++ copmiler (the current 7.30).  In egcs-19990629 on a
i386 Linux-2.2.10 / glibc-2.1.1 machine, it says:

$ g++ -o n n.C
n.C:15: warning: friend declaration `void TFunc(SomeClass<T> &)'
n.C:15: warning:   declares a non-template function
n.C:15: warning:   (if this is not what you intended, make sure
n.C:15: warning:   the function template has already been declared,
n.C:15: warning:   and add <> after the function name here)
n.C:15: warning:   -Wno-non-template-friend disables this warning.
/tmp/ccZPUmUN.o: In function `main':
/tmp/ccZPUmUN.o(.text+0x1d): undefined reference to `TFunc(SomeClass<int> &)'
collect2: ld returned 1 exit status

Adding the <> as the warning suggests produces a working compile in egcs, but
breaks all the other compilers (parse errors).  I'm happy to ignore the
warning or turn it off with the indicated switch.

The actual bug appears to be that the global template f'n never gets
instantiated even though it's quite clearly called from main().

Anyway, that seemed broken enough that I thought I'd mail it in.  :)

-mcq

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]