home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / gnu / gcc / bug / 2743 < prev    next >
Encoding:
Text File  |  1992-11-12  |  7.1 KB  |  273 lines

  1. Newsgroups: gnu.gcc.bug
  2. Path: sparky!uunet!convex!darwin.sura.net!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!igor.rational.COM!jdart
  3. From: jdart@igor.rational.COM (Jon Dart)
  4. Subject: template bug in gcc 2.3.1 (core dump)
  5. Message-ID: <9211121907.AA05971@igor.Rational.COM>
  6. Sender: gnulists@ai.mit.edu
  7. Organization: Gnus Not Usenet
  8. Distribution: gnu
  9. Date: Thu, 12 Nov 1992 19:07:15 GMT
  10. Approved: bug-gcc@prep.ai.mit.edu
  11. Lines: 260
  12.  
  13. The following code core-dumps g++ version 2.3.1, running on a SPARCstation 2
  14. under Sun OS 4.1.1.  I believe the code is correct and should compile.
  15. The messages output are:
  16.  
  17. array.hh: In function `int  main (int, char **)':
  18. array.hh:56: inconsistent return types for method `Array' in class `Array<int ,i
  19. nt>'
  20. array.hh:56: inconsistent return types for method `Array' in class `Array<int ,i
  21. nt>'
  22. /usr/local/bin/gcc: Internal compiler error: program cc1plus got fatal signal 11
  23.  
  24.  
  25. Apparently it does not recognize the constructor as a constructor.
  26.  
  27. ------------ code follows -------------
  28. #ifndef _SUPPORT_BOOLEAN_H
  29. #define _SUPPORT_BOOLEAN_H
  30.  
  31. class Boolean
  32. {
  33.  public:
  34.   inline Boolean ();
  35.   // default constructor (result is False)
  36.    
  37.   inline Boolean (const long);
  38.   inline operator long () const;
  39.   // Convert between Boolean and long
  40.  
  41.  private:
  42.    long Bit;
  43. };
  44.  
  45. extern const Boolean& True;
  46. extern const Boolean& False;
  47.  
  48. inline Boolean::Boolean ()
  49.   : Bit (0)
  50. {}
  51.  
  52. inline Boolean::Boolean (const long That)
  53.   : Bit (That != 0)
  54. {}
  55.  
  56. inline Boolean::operator long () const 
  57. {
  58.     return Bit; 
  59. }
  60.  
  61. #endif
  62.  
  63. #include <stddef.h>
  64. #include <assert.h>
  65. #include "boolean.hh"
  66.  
  67. template <class Contents, class Index >
  68. class Array 
  69. {
  70. public:
  71.       // Defines a simple, resizable-length array.  Copy and
  72.       // assignment operators create duplicate copies of array
  73.       // elements; i.e. there is no data-sharing across array
  74.       // instances.
  75.  
  76.   Array( const Index initial_size );
  77.  
  78.   virtual ~Array();
  79.   Array( const Array & );
  80.   Array & operator = (const Array & );
  81.  
  82.   Boolean      operator == (const Array &) const;
  83.   Boolean      operator != (const Array &) const;
  84.       // These functions assume that Contents has a meaningful ==
  85.       // operator
  86.  
  87.   virtual Contents &operator [] (const Index &) const;
  88.       // Note: we return a reference so that Array[k] = stuff works.
  89.       // An out-of-range index calls constraint_error, below
  90.  
  91.   int size() const
  92.   {
  93.     return data_size;
  94.   }
  95.  
  96.   virtual void resize( const Index new_size );
  97.       // The default behavior is to allocate a new array each
  98.       // time this is called.  This is not suitable if one wants
  99.       // to frequently expand the array by small increments.
  100.       // That's why it's virtual: so you can do something different.
  101.  
  102.   Array &operator += (const Contents &Data );
  103.   Array &operator += (const Array &Data );
  104.  
  105.   friend Array operator + (const Array&, const Contents &);
  106.   friend Array operator + (const Contents &, const Array&);
  107.   friend Array operator + (const Array&, const Array&);
  108.  
  109.   virtual void constraint_error(const Index index, const Index size);
  110.       // Called when an index exceeds the bounds of the array.
  111.       // The base version causes an assertion failure (may be
  112.       // disabled by compiling with -DNDEBUG).
  113.  
  114. protected:
  115.   Contents *data;
  116.   Index data_size;
  117. };
  118.  
  119. template <class Contents, class Index>
  120. Array<Contents,Index>::Array( const Index initial_size )
  121. {
  122.   data_size = initial_size;
  123.   data = new Contents [initial_size];
  124. }
  125.  
  126. template <class Contents, class Index>
  127. Array<Contents,Index>::~Array()
  128. {
  129.   delete [] data;
  130. }
  131.  
  132. template <class Contents, class Index>
  133. Array<Contents,Index>::Array( const Array<Contents,Index> &arr)
  134. {
  135.     data_size = arr.data_size;
  136.     data = new Contents [arr.length()];
  137.     for (Index i = 0; i < data_size; i++)
  138.       data[i] = arr.data[i];
  139. }
  140.  
  141. template <class Contents, class Index>
  142. Array <Contents,Index>& Array<Contents,Index>::operator = (const Array<Contents,Index> &arr)
  143. {
  144.   if (this != &arr)
  145.   {
  146.     data_size = arr.data_size;
  147.     data = new Contents [arr.length()];
  148.     for (Index i = 0; i < data_size; i++)
  149.       data[i] = arr.data[i];
  150.   }
  151.   return *this;
  152. }
  153.  
  154. template <class Contents, class Index>
  155. Boolean Array<Contents,Index>::operator == (const Array<Contents,Index> & arr) const
  156. {
  157.   if (arr.data_size != data_size)
  158.     return False;
  159.   for (Index i = 0; i<data_size;i++)
  160.     if (arr.data[i] != data[i])
  161.       return False;
  162.   return True;
  163. }
  164.  
  165. template <class Contents, class Index>
  166. Boolean Array<Contents,Index>::operator != (const Array<Contents,Index> & arr) const
  167. {
  168.   if (arr.data_size != data_size)
  169.     return True;
  170.   for (Index i = 0; i<data_size;i++)
  171.     if (arr.data[i] != data[i])
  172.       return True;
  173.   return False;
  174. }
  175.  
  176. template <class Contents, class Index>
  177. Contents& Array<Contents,Index>::operator [](const Index &index) const
  178. {
  179. #ifndef NDEBUG
  180.    if (index < 0 || index >= data_size)
  181.    {
  182.       constraint_error(index,data_size);      
  183.    }
  184. #endif
  185.    return data[index];
  186. }
  187.  
  188. template <class Contents, class Index>
  189. Array<Contents,Index> &Array<Contents,Index>::operator += (const Contents &Data )
  190. {
  191.    // not very efficient
  192.    Index old_data_size = data_size;
  193.    resize(data_size + 1);
  194.    data[old_size] = Data;
  195.    return *this;
  196. }
  197.  
  198. template <class Contents, class Index>
  199. Array<Contents,Index> &Array<Contents,Index>::operator += (const Array <Contents,Index>&Data )
  200. {
  201.    Index old_size = data_size;
  202.    resize(data_size + Data.size);
  203.    for (Index i = old_size; i < data_size; i++)
  204.      data[i] = Data.data[i-old_size];
  205.    return *this;
  206. }
  207.  
  208. template <class Contents, class Index>
  209. void Array<Contents,Index>::resize( const Index new_size )
  210. {
  211.    Contents * new_data = new Contents[new_size];
  212.    assert(new_data);
  213.    for (Index i = 0; i<data_size; i++)
  214.       new_data[i] = data[i];
  215.    delete [] data;
  216.    data = new_data; 
  217.    data_size = new_size;
  218. }
  219.  
  220. template <class Contents, class Index>
  221. Array<Contents,Index> operator + (const Array<Contents,Index>& arr, const Contents &c)
  222. {
  223.    Array<Contents,Index> ret_val(arr.length() + 1);
  224.    for (Index i=0;i<arr.length();i++)
  225.       ret_val.data[i] = arr.data[i];
  226.    ret_val.data[arr.length()] = c;
  227.    return ret_val;
  228. }
  229.  
  230. template <class Contents, class Index>
  231. Array<Contents,Index> operator + (const Contents &c, const Array<Contents,Index>&arr)
  232. {
  233.    Array<Contents,Index> ret_val(arr.length() + 1);
  234.    for (Index i=0;i<arr.length();i++)
  235.       ret_val.data[i] = arr.data[i];
  236.    ret_val.data[arr.length()] = c;
  237.    return ret_val;
  238. }
  239.  
  240. template <class Contents, class Index>
  241. Array<Contents,Index> operator + (const Array<Contents,Index>&arr1, 
  242.                 const Array<Contents,Index>&arr2)
  243. {
  244.    Array<Contents,Index> ret_val(arr1.length() + arr2.length());
  245.    for (Index i=0;i<arr1.length();i++)
  246.       ret_val.data[i] = arr2.data[i];
  247.    const Index start = arr1.length();
  248.    for (i=start;i<start + arr2.length();i++)
  249.       ret_val.data[i] = arr2.data[i-start];
  250.    return ret_val;
  251. }
  252.  
  253. template <class Contents, class Index>
  254. void Array<Contents,Index>::constraint_error(const Index index,
  255.                        const Index limit)
  256. {
  257.   assert(index >= 0 && index < limit);
  258. }
  259.  
  260. #include "array.hh"
  261. #include <iostream.h>
  262.  
  263. main(int argc, char**argv)
  264. {
  265.   Array<int,int> stuff( 10 );
  266.   for (int i=0;i<stuff.size();i++)
  267.   {
  268.     stuff[i] = i*i - 8*i + 5;
  269.     cout << "stuff[" << i << "] = " << stuff[i] << endl;
  270.   }
  271. }
  272.  
  273.