This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


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

g++ template bugs (EGCS 1.0)?


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



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