This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Help on class templates + friendship
- To: gcc at gcc dot gnu dot org
- Subject: Help on class templates + friendship
- From: Frederic Perez <frederic at ima dot udg dot es>
- Date: Fri, 29 Oct 1999 16:15:36 +0200
Hello everyone,
While porting a program to Linux using GCC (from SGI using MIPSpro)
I'm having troubles trying to establish a friendship between
two class templates, and I still don't know if this is due to
a restriction on the GCC compiler or to a wrong use of notation.
What follows is an over-simplified version of the code that fails
to compile:
--------------------
template<typename TV> class C; // forward declaration
template<typename TS, typename TV>
class B {
public:
B(int i=0) : i_(i) {}
B& operator+=(const B& rhs) { i_ += rhs.i_; return *this; }
B& operator+=(const C<TV>& rhs) { i_ += rhs.j_; return *this; }
private:
int i_;
};
template<typename TV>
class C {
public:
template<typename TS, TV> friend class B; //makes it crash using gcc-2.51.1
C(int j=0) : j_(j) {}
private:
int j_;
};
int main() {
B<int, float> foo;
C<float> bar;
foo += bar;
}
--------------------
g++ -v -o test Test.C
Reading specs from /scratch/gcc-2.95.1/lib/gcc-lib/i686-pc-linux-gnu/2.95.1/specs
gcc version 2.95.1 19990816 (release)
/scratch/gcc-2.95.1/lib/gcc-lib/i686-pc-linux-gnu/2.95.1/cpp -lang-c++ -v
-D__GNUC__=2 -D__GNUG__=2 -D__GNUC_MINOR__=95 -D__cplusplus -D__ELF__ -Dunix
-D__i386__ -Dlinux -D__ELF__ -D__unix__ -D__i386__ -D__linux__ -D__unix -D__linux
-Asystem(posix) -D__EXCEPTIONS -Acpu(i386) -Amachine(i386) -Di386 -D__i386 -D__i386__
-Di686 -Dpentiumpro -D__i686 -D__i686__ -D__pentiumpro -D__pentiumpro__ Test.C
/tmp/ccTivoOX.ii
GNU CPP version 2.95.1 19990816 (release) (i386 Linux/ELF)
#include "..." search starts here:
#include <...> search starts here:
/scratch/gcc-2.95.1/lib/gcc-lib/i686-pc-linux-gnu/2.95.1/../../../../include/g++-3
/usr/local/include
/scratch/gcc-2.95.1/lib/gcc-lib/i686-pc-linux-gnu/2.95.1/../../../../i686-pc-linux-gnu/include
/scratch/gcc-2.95.1/lib/gcc-lib/i686-pc-linux-gnu/2.95.1/include
/usr/include
End of search list.
The following default directories have been omitted from the search path:
End of omitted list.
/scratch/gcc-2.95.1/lib/gcc-lib/i686-pc-linux-gnu/2.95.1/cc1plus /tmp/ccTivoOX.ii
-quiet -dumpbase Test.cc -version -o /tmp/ccsMbzqO.s
GNU C++ version 2.95.1 19990816 (release) (i686-pc-linux-gnu) compiled by GNU C
version 2.95.1 19990816 (release).
Test.C: In instantiation of `C<float>':
Test.C:24: instantiated from here
Test.C:3: template parameter `class TV'
Test.C:14: redeclared here as `float {anon}'
gmake: *** [phony] Error 1
Any hint or pointer to help will be appreciated.
Thanks in advance,
Frederic