[patch] typename tweaks

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[patch] typename tweaks

Mumit Khan-4
Get rid of those annoying ``implicit typename'' warnings. All these years
of C++, and I still can't get these right. Argh. GCC-3.1 seems to think
that some of the typenames in Array.h can be omitted, but I don't believe
that's correct (sorry, can't quote chapter and verse right now).

Checked against gcc-3.1, someone should definitely check against 2.95.x
to make sure these changes are ok.

liboctave/ChangeLog:

2002-06-05  Mumit Khan  <[hidden email]>

        * Array-idx.h, Array.cc, Array.h, Array2-idx.h, Array2.cc, Array3.cc,
        ArrayN.cc, DiagArray2.cc: Add typename where appropriate.

Index: Array-idx.h
===================================================================
RCS file: /cvs/octave/liboctave/Array-idx.h,v
retrieving revision 1.29
diff -u -3 -p -r1.29 Array-idx.h
--- Array-idx.h 2002/05/15 03:21:00 1.29
+++ Array-idx.h 2002/06/06 04:15:04
@@ -165,7 +165,7 @@ Array<T>::maybe_delete_elements (idx_vec
       if (--rep->count <= 0)
  delete rep;

-      rep = new ArrayRep (new_data, new_len);
+      rep = new typename Array<T>::ArrayRep (new_data, new_len);

       set_max_indices (1);
     }
Index: Array.cc
===================================================================
RCS file: /cvs/octave/liboctave/Array.cc,v
retrieving revision 1.49
diff -u -3 -p -r1.49 Array.cc
--- Array.cc 2002/05/15 03:21:00 1.49
+++ Array.cc 2002/06/06 04:15:04
@@ -48,7 +48,7 @@ Software Foundation, 59 Temple Place - S
 template <class T>
 Array<T>::Array (int n, const T& val)
 {
-  rep = new ArrayRep (n);
+  rep = new typename Array<T>::ArrayRep (n);

   for (int i = 0; i < n; i++)
     rep->data[i] = val;
@@ -105,11 +105,11 @@ Array<T>::resize (int n)
   if (n == length ())
     return;

-  ArrayRep *old_rep = rep;
+  typename Array<T>::ArrayRep *old_rep = rep;
   const T *old_data = data ();
   int old_len = length ();

-  rep = new ArrayRep (n);
+  rep = new typename Array<T>::ArrayRep (n);

   if (old_data && old_len > 0)
     {
@@ -136,11 +136,11 @@ Array<T>::resize (int n, const T& val)
   if (n == length ())
     return;

-  ArrayRep *old_rep = rep;
+  typename Array<T>::ArrayRep *old_rep = rep;
   const T *old_data = data ();
   int old_len = length ();

-  rep = new ArrayRep (n);
+  rep = new typename Array<T>::ArrayRep (n);

   int min_len = old_len < n ? old_len : n;

@@ -164,7 +164,7 @@ Array<T>::fortran_vec (void)
   if (rep->count > 1)
     {
       --rep->count;
-      rep = new ArrayRep (*rep);
+      rep = new typename Array<T>::ArrayRep (*rep);
     }
   return rep->data;
 }
Index: Array.h
===================================================================
RCS file: /cvs/octave/liboctave/Array.h,v
retrieving revision 1.61
diff -u -3 -p -r1.61 Array.h
--- Array.h 2002/05/23 17:18:12 1.61
+++ Array.h 2002/06/06 04:15:04
@@ -96,7 +96,7 @@ protected:
       if (rep->count > 1)
  {
   --rep->count;
-  rep = new ArrayRep (*rep);
+  rep = new typename Array<T>::ArrayRep (*rep);
  }
     }

@@ -108,11 +108,11 @@ protected:

 protected:

-  ArrayRep *rep;
+  typename Array<T>::ArrayRep *rep;

   Array (T *d, int l)
     {
-      rep = new ArrayRep (d, l);
+      rep = new typename Array<T>::ArrayRep (d, l);

 #ifdef HEAVYWEIGHT_INDEXING
       idx = 0;
@@ -125,7 +125,7 @@ public:

   Array (void)
     {
-      rep = new ArrayRep ();
+      rep = new typename Array<T>::ArrayRep ();

 #ifdef HEAVYWEIGHT_INDEXING
       idx = 0;
@@ -136,7 +136,7 @@ public:

   explicit Array (int n)
     {
-      rep = new ArrayRep (n);
+      rep = new typename Array<T>::ArrayRep (n);

 #ifdef HEAVYWEIGHT_INDEXING
       idx = 0;
Index: Array2-idx.h
===================================================================
RCS file: /cvs/octave/liboctave/Array2-idx.h,v
retrieving revision 1.35
diff -u -3 -p -r1.35 Array2-idx.h
--- Array2-idx.h 2002/05/15 03:21:00 1.35
+++ Array2-idx.h 2002/06/06 04:15:04
@@ -281,7 +281,7 @@ Array2<T>::maybe_delete_elements (idx_ve
   if (--rep->count <= 0)
     delete rep;

-  rep = new ArrayRep (new_data, new_n);
+  rep = new typename Array<T>::ArrayRep (new_data, new_n);

   if (nr == 1)
     {
@@ -395,7 +395,8 @@ Array2<T>::maybe_delete_elements (idx_ve
       if (--rep->count <= 0)
  delete rep;

-      rep = new ArrayRep (new_data, nr * new_nc);
+      rep = new typename
+        Array<T>::ArrayRep (new_data, nr * new_nc);

       d2 = new_nc;

@@ -459,7 +460,8 @@ Array2<T>::maybe_delete_elements (idx_ve
       if (--rep->count <= 0)
  delete rep;

-      rep = new ArrayRep (new_data, new_nr * nc);
+      rep = new typename
+        Array<T>::ArrayRep (new_data, new_nr * nc);

       d1 = new_nr;

Index: Array2.cc
===================================================================
RCS file: /cvs/octave/liboctave/Array2.cc,v
retrieving revision 1.15
diff -u -3 -p -r1.15 Array2.cc
--- Array2.cc 2002/05/15 03:21:00 1.15
+++ Array2.cc 2002/06/06 04:15:04
@@ -122,14 +122,14 @@ Array2<T>::resize (int r, int c)
   if (r == dim1 () && c == dim2 ())
     return;

-  Array<T>::ArrayRep *old_rep = rep;
+  typename Array<T>::ArrayRep *old_rep = rep;
   const T *old_data = data ();

   int old_d1 = dim1 ();
   int old_d2 = dim2 ();
   int old_len = length ();

-  rep = new Array<T>::ArrayRep (get_size (r, c));
+  rep = new typename Array<T>::ArrayRep (get_size (r, c));

   d1 = r;
   d2 = c;
@@ -162,14 +162,14 @@ Array2<T>::resize (int r, int c, const T
   if (r == dim1 () && c == dim2 ())
     return;

-  Array<T>::ArrayRep *old_rep = rep;
+  typename Array<T>::ArrayRep *old_rep = rep;
   const T *old_data = data ();

   int old_d1 = dim1 ();
   int old_d2 = dim2 ();
   int old_len = length ();

-  rep = new Array<T>::ArrayRep (get_size (r, c));
+  rep = new typename Array<T>::ArrayRep (get_size (r, c));

   d1 = r;
   d2 = c;
Index: Array3.cc
===================================================================
RCS file: /cvs/octave/liboctave/Array3.cc,v
retrieving revision 1.9
diff -u -3 -p -r1.9 Array3.cc
--- Array3.cc 2000/02/01 04:06:08 1.9
+++ Array3.cc 2002/06/06 04:15:04
@@ -58,7 +58,7 @@ Array3<T>::resize (int r, int c, int p)
   if (r == dim1 () && c == dim2 () && p == dim3 ())
     return;

-  ArrayRep *old_rep = rep;
+  typename Array<T>::ArrayRep *old_rep = rep;
   const T *old_data = data ();

   int old_d1 = dim1 ();
@@ -68,7 +68,7 @@ Array3<T>::resize (int r, int c, int p)

   int ts = get_size (get_size (r, c), p);

-  rep = new ArrayRep (ts);
+  rep = new typename Array<T>::ArrayRep (ts);

   d1 = r;
   d2 = c;
@@ -104,7 +104,7 @@ Array3<T>::resize (int r, int c, int p,
   if (r == dim1 () && c == dim2 () && p == dim3 ())
     return;

-  ArrayRep *old_rep = rep;
+  typename Array<T>::ArrayRep *old_rep = rep;
   const T *old_data = data ();

   int old_d1 = dim1 ();
@@ -115,7 +115,7 @@ Array3<T>::resize (int r, int c, int p,

   int ts = get_size (get_size (r, c), p);

-  rep = new ArrayRep (ts);
+  rep = new typename Array<T>::ArrayRep (ts);

   d1 = r;
   d2 = c;
Index: ArrayN.cc
===================================================================
RCS file: /cvs/octave/liboctave/ArrayN.cc,v
retrieving revision 1.3
diff -u -3 -p -r1.3 ArrayN.cc
--- ArrayN.cc 2002/05/03 19:56:01 1.3
+++ ArrayN.cc 2002/06/06 04:15:04
@@ -227,10 +227,10 @@ ArrayN<T>::resize (const Array<int>& dim
   if (no_change)
     return;

-  Array<T>::ArrayRep *old_rep = rep;
+  typename Array<T>::ArrayRep *old_rep = rep;
   const T *old_data = data ();

-  rep = new Array<T>::ArrayRep (get_size (dims));
+  rep = new typename Array<T>::ArrayRep (get_size (dims));

   Array<int> old_dimensions = dimensions;

@@ -282,12 +282,12 @@ ArrayN<T>::resize (const Array<int>& dim
   if (no_change)
     return;

-  Array<T>::ArrayRep *old_rep = rep;
+  typename Array<T>::ArrayRep *old_rep = rep;
   const T *old_data = data ();

   int len = get_size (dims);

-  rep = new Array<T>::ArrayRep (len);
+  rep = new typename Array<T>::ArrayRep (len);

   Array<int> old_dimensions = dimensions;

Index: DiagArray2.cc
===================================================================
RCS file: /cvs/octave/liboctave/DiagArray2.cc,v
retrieving revision 1.6
diff -u -3 -p -r1.6 DiagArray2.cc
--- DiagArray2.cc 2002/05/03 19:56:01 1.6
+++ DiagArray2.cc 2002/06/06 04:15:04
@@ -134,13 +134,13 @@ DiagArray2<T>::resize (int r, int c)
   if (r == dim1 () && c == dim2 ())
     return;

-  ArrayRep *old_rep = rep;
+  typename Array<T>::ArrayRep *old_rep = rep;
   const T *old_data = data ();
   int old_len = length ();

   int new_len = r < c ? r : c;

-  rep = new ArrayRep (new_len);
+  rep = new typename Array<T>::ArrayRep (new_len);

   nr = r;
   nc = c;
@@ -170,13 +170,13 @@ DiagArray2<T>::resize (int r, int c, con
   if (r == dim1 () && c == dim2 ())
     return;

-  ArrayRep *old_rep = rep;
+  typename Array<T>::ArrayRep *old_rep = rep;
   const T *old_data = data ();
   int old_len = length ();

   int new_len = r < c ? r : c;

-  rep = new ArrayRep (new_len);
+  rep = new typename Array<T>::ArrayRep (new_len);

   nr = r;
   nc = c;


Reply | Threaded
Open this post in threaded view
|

[patch] typename tweaks

John W. Eaton-6
On  5-Jun-2002, Mumit Khan <[hidden email]> wrote:

| Get rid of those annoying ``implicit typename'' warnings. All these years
| of C++, and I still can't get these right. Argh. GCC-3.1 seems to think
| that some of the typenames in Array.h can be omitted, but I don't believe
| that's correct (sorry, can't quote chapter and verse right now).
|
| Checked against gcc-3.1, someone should definitely check against 2.95.x
| to make sure these changes are ok.
|
| liboctave/ChangeLog:
|
| 2002-06-05  Mumit Khan  <[hidden email]>
|
| * Array-idx.h, Array.cc, Array.h, Array2-idx.h, Array2.cc, Array3.cc,
| ArrayN.cc, DiagArray2.cc: Add typename where appropriate.

Thanks.

I made these changes today.  Unfortunately, because I haven't been
very good at keeping up with bug reports, I had to rediscover this all
on my own when someone reported some new warnings with gcc 3.2.

Then when I did a quick search for "new typename" to see if that would
be valid syntax, I found your message.  This was just before I was
going to send you mail and ask for your advice.  And so it happens
that you had already answered the question that I was about to ask.
:-)

BTW, it looks like g++ 3.2 also causes code like

  std::ostrstream buf;
  buf << "put some stuff in buf" << std::ends;
  char *s = buf.str ();
  do_something_with (s);
  delete [] s;

to fail.  It's the delete that causes trouble.  Apparently this was
never guaranteed to work, even though I never saw anything but this
idiom a few years ago.  Now you are supposed to write

  buf.freeze (false);

to tell the ostrstream that it is once again responsible for freeing
the memory.  I don't remember ever seeing this method recommended in
any C++ book, but then I may have missed a few pages.

Anyway, I've added a check for <sstream> in configure.in and also the
new header file liboctave/lo-sstream.h:

  #if !defined (octave_liboctave_sstream_h)
  #define octave_liboctave_sstream_h 1

  #ifdef HAVE_SSTREAM

  #include <sstream>

  #define STRINGSTREAMBUF std::stringbuf

  #define ISSTREAM std::istringstream

  #define OSSTREAM std::ostringstream
  #define OSSTREAM_STR(os) (os).str ()
  #define OSSTREAM_C_STR(os) (os).str () . c_str ()
  #define OSSTREAM_ENDS ""
  #define OSSTREAM_FREEZE(os) do { } while (0)

  #else

  #include <strstream>

  #define STRINGSTREAMBUF std::strstreambuf

  #define ISSTREAM std::istrstream

  #define OSSTREAM std::ostrstream
  #define OSSTREAM_STR(os) std::string ((os).str ())
  #define OSSTREAM_C_STR(os) (os).str ()
  #define OSSTREAM_ENDS std::ends
  #define OSSTREAM_FREEZE(os) do { (os).freeze (false); } while (0)

  #endif

  #endif

With this, I've also changed the code to use the lo-sstream.h header
instead of <strstream> and to use the macros instead of manipulating
strstreams or stringstreams directly.  It seems to work.

Note that istrstreams and strstreambufs are not used much, so I think
what is shown above is sufficient for Octave.  There is one additional
#ifdef HAVE_SSTREAM conditional in the source.

Someday, we can delete all of this.  But since gcc 2.95 (without
sstream) is still commonly used, I don't think we should do it quite
yet.

jwe


Reply | Threaded
Open this post in threaded view
|

Re: [patch] typename tweaks

Mumit Khan-4
On Sat, 17 Aug 2002, John W. Eaton wrote:

> Then when I did a quick search for "new typename" to see if that would
> be valid syntax, I found your message.  This was just before I was
> going to send you mail and ask for your advice.  And so it happens
> that you had already answered the question that I was about to ask.
> :-)

I had forgotten all about it, thanks for getting to it.

> BTW, it looks like g++ 3.2 also causes code like
>
>   std::ostrstream buf;
>   buf << "put some stuff in buf" << std::ends;
>   char *s = buf.str ();
>   do_something_with (s);
>   delete [] s;

Odd. The .str() method implicitly implies .freeze(true), so that it's
the user's responsibility to free the character buffer, so I can't
see where the problem is. Then again, I haven't used ostrstream in
years, so have conveniently the subtle nuances.

> to fail.  It's the delete that causes trouble.  Apparently this was
> never guaranteed to work, even though I never saw anything but this
> idiom a few years ago.  Now you are supposed to write
>
>   buf.freeze (false);
>
> to tell the ostrstream that it is once again responsible for freeing
> the memory.  I don't remember ever seeing this method recommended in
> any C++ book, but then I may have missed a few pages.

Isn't that double free'ing here? Let me take a look at the specs for
strstream and see if I understand what's going on.

> Anyway, I've added a check for <sstream> in configure.in and also the
> new header file liboctave/lo-sstream.h:

Excellent.

> Someday, we can delete all of this.  But since gcc 2.95 (without
> sstream) is still commonly used, I don't think we should do it quite
> yet.

Well, the other choice is to include a version of sstream (eg., the
one from Dietmar Kuehl) with Octave that switch on and off depending
on the runtime library.

Mumit