public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* g++ template bugs (EGCS 1.0)?
@ 1997-12-17  4:48 Brendt Wohlberg
  0 siblings, 0 replies; only message in thread
From: Brendt Wohlberg @ 1997-12-17  4:48 UTC (permalink / raw)
  To: egcs

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


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~1997-12-17  4:48 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-12-17  4:48 g++ template bugs (EGCS 1.0)? Brendt Wohlberg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).