Templates
Erez Louidor Lior
s3824888@techst02.technion.ac.il
Thu Nov 26 15:05:00 GMT 1998
I've found 2 bugs using templates.
1.
Egcs doesn't instantiate const static variable arrays. For example:
#include <iostream>
template <class T>
class A{
public:
ÃÂ static const int l[5];
};
template<class T>
const int A<T>::l[5] = {1, 2, 3, 4, 5};
int main(){
ÃÂ cout << A<int>::l[1];
}
#include <iostream>
template <class T>
class A{
public:
ÃÂ static const int l[5];
};
eg++ -I /usr/local/egcs/include/g++-2/ static-array.cc
/tmp/ccb2YaIs.o: In function `main':
/tmp/ccb2YaIs.o(.text+0x4): undefined reference to `A<int>::l'
collect2: ld returned 1 exit status
Removing the const in l's declaration and definition solves the problem.
2.ÃÂ There is an instantiation problem in the template repository.
When using -frepo, in the compilation stage, te compiler only emits
symbols
that require instantiation from non-template code. If instantiating
these symbols will
require futher symbols to be instantiated these required symbols are
not
emitted into the
repository. For example:
#include <iostream>
template <class T>
class foo{
public:
ÃÂ void g();
ÃÂ void h();
};
template <class T>
void foo<T>::g() {
ÃÂ h();
}
template <class T>
void foo<T>::h() {
ÃÂ cout << "foo::h called" << endl;
}
int main() {
ÃÂ foo<int> f;
ÃÂ f.g();
}
Here, the instantiation of g will require h to be instantiated as well,
but
the compiler only emits to the repository, that g needs to be
instantiated.
eg++ -I /usr/local/egcs/include/g++-2/ -frepo -c foo.cc
foo.rpo: (after compilation, before link)
M foo.cc
D /home/Erez/C++/compilers/egcs
A '-I' '/usr/local/egcs/include/g++-2/' '-frepo' '-c'
O g__t3foo1Zi
eg++ -frepo foo.o
collect: recompiling foo.cc
collect: relinking
foo.o: In function `foo<int>::g(void)':
foo.o(.text+0x25): undefined reference to `foo<int>::h(void)'
collect2: ld returned 1 exit status
foo.rpo: (after linking)
M foo.cc
D /home/Erez/C++/compilers/egcs
A '-I' '/usr/local/egcs/include/g++-2/' '-frepo' '-c'
O h__t3foo1Zi
C g__t3foo1Zi
I was using egcs on linux. Also I was using binutils 2.9.1 that were
built in the same
directory I compiled egcs, using egcs' makefile.
eg++ -v produced:
Reading specs from
/usr/local/egcs-19981005/lib/gcc-lib/i586-pc-linux-gnulibc1/egcs-2.92.13/specs
gcc version egcs-2.92.13 19981005 (gcc2 ss-980609 experimental)
ÃÂ
ÃÂ ÃÂ ÃÂ Hope this helps...
ÃÂ ÃÂ ÃÂ Erez.
--ÃÂ
----------------------------------------------------------------------
"The ships hung in the sky in much the same way that bricks don't."
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ -- Douglas Adams / The Hitchhiker "Trilogy".
ÃÂ
More information about the Gcc-bugs
mailing list