From: jbg@qualcomm.com (Jeff Gehlhaar)
Subject: Template bug, v2.3.2
Organization: GNUs Not Usenet
Distribution: gnu
Date: Fri, 11 Dec 1992 09:23:48 GMT

The following code illustrates what appears to be a template bug and/or an
iostream problem.

The template class is a vector. One version is created that takes a vector
as an arguement, thus creating a vector of a vector.  When you try and
print it, an output operator is provided (<< operator), and it uses another
operator ( [] ) to get the data item from the class. In the case of a
vector of a vector, the returned value should once again cause a call to
the << operator, on a vector item. It does not.


::::::::::::::
compiles but does not run correctly (would run correctly if the operator <<
were a friend function of the class vector)
::::::::::::::
::::::::::::::
test.c
::::::::::::::
#include <stream.h>

/***************************************************************************
***
 * vector template definition
 ***************************************************************************
***/
template<class T> class vector {
private:
  T v[2];
public:
     vector(void)              {}
     vector(const T& y);

  T& operator [] (int i) const { return v[i]; }
};
template<class T> ostream& operator << (ostream& s, const vector<T>& x);


/***************************************************************************
***
 * vector template routines
 ***************************************************************************
***/
template <class T> vector<T>::vector(const T& y)
{
  for (int i = 0; i < 2; i++) v[i] = y;
}
template <class T> ostream& operator << (ostream& s, const vector<T>& x)
{
  for (int i = 0; i < 2; i++) s << x[i] << " ";
  return s;
}


/***************************************************************************
***
 * main routine
 ***************************************************************************
***/
int main(void)
{
  vector<int>           x = vector<int>(1);
  vector< vector<int> > u = vector< vector<int> >(vector<int>(1));

  cout << "x:    " << x    << "\n";
  cout << "u:    " << u    << "\n";
}
::::::::::::::
compilation output (g++ -v test.c -o test)
::::::::::::::
Reading specs from /usr/local/lib/gcc-lib/sparc-sun-sunos4.1/2.3.2/specs
gcc version 2.3.2
 /usr/local/lib/gcc-lib/sparc-sun-sunos4.1/2.3.2/cpp -lang-c++ -v -undef
-D__GNUC__=2 -D__GNUG__=2 -D__cplusplus -Dsparc -Dsun -Dunix -D__sparc__
-D__sun__ -D__unix__ -D__sparc -D__sun -D__unix test.c /usr/tmp/cca10060.i
GNU CPP version 2.3.2 (sparc)
 /usr/local/lib/gcc-lib/sparc-sun-sunos4.1/2.3.2/cc1plus
/usr/tmp/cca10060.i -quiet -dumpbase test.cc -version -o
/usr/tmp/cca10060.s
GNU C++ version 2.3.2 (sparc) compiled by CC.
 as -o /usr/tmp/cca100601.o /usr/tmp/cca10060.s
 /usr/local/lib/gcc-lib/sparc-sun-sunos4.1/2.3.2/ld -e start -dc -dp -o
test /lib/crt0.o -L/usr/local/lib/gcc-lib/sparc-sun-sunos4.1/2.3.2
-L/usr/local/lib /usr/tmp/cca100601.o -lg++ -lgcc -lc -lgcc
::::::::::::::
run output  (u should be "1 1  1 1 ")
::::::::::::::
x:    1 1 
u:    1 1 



======================================================
Jeff Gehlhaar           Qualcomm Inc,  
jbg@qualcomm.com        San Diego, CA, USA
(619) 597-5241          "The Elegant Solution...."
======================================================


Newsgroups: gnu.groff.bug