From: jrauser@mermaid.micro.umn.edu (John Rauser)
Newsgroups: gnu.g++.bug
Subject: Bug in g++-2.3.3
Date: 26 Jan 1993 22:22:32 -0500
Organization: GNUs Not Usenet
Distribution: gnu

Hiya...

There seems to be a big squishy bug in g++-2.3.3:

Script started on Tue Jan 26 15:21:23 1993

3:21pm ~>g++ -v
Reading specs from /usr/gnu/lib/gcc-lib/sparc/2.3.3/specs
gcc version 2.3.3
3:21pm ~>exit
exit

script done on Tue Jan 26 15:21:43 1993


I was playing around, trying to learn how to use templates and I
decided to implement a stack.  So, I wrote a stack template that
had a constructor which took as an argument, an integer that was to
be the maximum size of the stack.  When I tried to compile it, I got
the following: (source listings follow at the end of the file)

3:17pm ~>g++ -Wall old-stack.cxx
old-stack.cxx: In function `int  main ()':
old-stack.cxx:13: inconsistent return types for method `stack' in class `stack<int>'
old-stack.cxx:13: inconsistent return types for method `stack' in class `stack<float>'
old-stack.cxx: In method `stack<float>::~stack ()':
old-stack.cxx:19: Internal compiler error.
old-stack.cxx:19: Please report this to `bug-g++@prep.ai.mit.edu'.


Determined, I tried various tricks to try and make the damn thing
compile... I found that if I changed the constructor so that it didn't
take any arguments, it compiled fine.  Still determined to get it to
work the way _I_ wanted (with arguments to the constructor), I tried to
put the body of the constructor inline in the class declaration, (I
tried to just use the inline directive, but that resulted in the same
error) and it compiled fine.  Now I had what I wanted... after much
hair-pulling.  

3:17pm ~>g++ -Wall stack.cxx
3:17pm ~>
Use "exit" to leave tcsh.
3:17pm ~>exit
exit


Hope this helps you folks out....

John Rauser
jrauser@staff.tc.umn.edu
jrauser@mermaid.micro.umn.edu
jmr@ddt.biochem.umn.edu
mf12003@sc.msc.edu

***************************************************************
************* Listing of stack.cxx       **********************
************* This is the one that works **********************
***************************************************************

#include<stdio.h>
#include<iostream.h>

template <class T> class stack {
 public:
  stack (int maxsize) 
    {
      data = new T[maxsize];  //This code not inline makes g++ puke.
      top = 0;
    }
 ~stack ();
  T		push (T item);
  T		pop ();
 private:
  int top;
  T* data;
};

template <class T> stack<T>::~stack () 
{
  delete data;
}

template <class T> T stack<T>::push (T item) 
{
   data[top++] = item;
   return item;
}

template <class T> T stack<T>::pop () 
{
  return data[--top];
}

int main () 
{
  stack<int> s1(20);
  stack<float> s2(20);

  s1.push (10);
  printf ("%d\n",s1.pop());

  s2.push (10.43);
  printf ("%f\n",s2.pop());

}
   


**************************************************************
********* Listing of old-stack.cxx ***************************
********* This one doesn't work    ***************************
**************************************************************
#include<stdio.h>
#include<iostream.h>

template <class T> class stack {
 public:
  stack (int maxsize);
 ~stack ();
  T		push (T item);
  T		pop ();
 private:
  int top;
  T* data;
};

template <class T> stack<T>::stack (int maxsize)
{
  data = new T[maxsize];
  top = 0;
}

template <class T> stack<T>::~stack () 
{
  delete data;
}

template <class T> T stack<T>::push (T item) 
{
   data[top++] = item;
   return item;
}

template <class T> T stack<T>::pop () 
{
  return data[--top];
}

int main () 
{
  stack<int> s1(20);
  stack<float> s2(20);

  s1.push (10);
  printf ("%d\n",s1.pop());

  s2.push (10.43);
  printf ("%f\n",s2.pop());

}