This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
g++ template bugs (EGCS 1.0)?
- To: egcs at cygnus dot com
- Subject: g++ template bugs (EGCS 1.0)?
- From: brendt at dip dot ee dot uct dot ac dot za (Brendt Wohlberg)
- Date: Wed, 17 Dec 1997 14:40:32 -0200
- Reply-To: egcs at cygnus dot com
The following examples appear to reveal bugs in the template mechanism:
1) The template (friend) function in the following example causes a
linker error unless compiled with -fname-mangling-version-0. Also,
when the explicit template instantiation is removed, and the code
is compiled in a single file, a linker error results for both
name mangling versions:
//File: a.h
#include <iostream.h>
template <class T>
class number {
private:
T num;
public:
explicit number(T n) { num = n; }
T operator()() const { return num; }
friend void print(const number<T>& n);
};
//File: a.cc
#include "a.h"
template <class T>
void print(const number<T>& n)
{
cout << n.num << endl;
}
template void print(const number<float>& n);
//File: b.cc
#include "a.h"
int main()
{
number<float> fnum(2.4);
print(fnum);
return 0;
}
2) Explicit instantiations don't appear to work for templates member
functions - the compiler gives an error for the following code:
//File: x.h
#include <iostream.h>
template <class T>
class number {
private:
T num;
public:
explicit number(T n) { num = n; }
T operator()() const { return num; }
print() { cout << num << endl; }
template <class S>
number(const number<S>& n);
};
//File: x.cc
#include "x.h"
template <class T>
template <class S>
number<T>::number(const number<S>& n)
{
num = n();
}
template number<float>::number(const number<int> &);
//File: y.cc
#include "x.h"
int main()
{
number<int> inum(2);
number<float> fnum = inum;
inum.print();
fnum.print();
return 0;
}
--
Brendt Wohlberg E-mail: brendt@dip.ee.uct.ac.za
Department of Electrical Engineering Phone : +27 21 650 3466
University of Cape Town Fax : +27 21 650 3465