🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: grads
Article #8

Re: Why doesn't this simple program compile?

#8
From: Scott Michel
Date: Fri, 01 Sep 1995 23:47
70 lines
2947 bytes
Ching-Tsun Chou (chou@cs.ucla.edu) wrote:

<soapbox mode activated>
Return the Lippman book from whence it came. It was written before
most compiler's even knew that templates existed. That particular
section is speculative. Even the ARM is out of date in this regard.
The long and the short of it is, Ditch The Book and Pay The Extra
Bucks For Something Copyright 1994 or 1995!
<soapbox mode deactivated>

(ObEditorial: Hey! Anyone got the ANSI C++ cmte's current spec in the
CSD? Could we get it for the archives?!?)

: template <class Type>
: void swap( Array<Type> &A, int i, int j )
: {
:   Type tmp = A[i]; A[i] = A[j]; A[j] = tmp;
: }

This'd be part of the problem. GCC 2.7.0 is still pretty weak when it
comes to templates (all the hard work going into it at Cygnus
notwithstanding). Part of the problem is that the compiler is not able
to "downcast" your ArrayRC to an Array because this really isn't or
shouldn't be allowed - what if the layout sizes of the ArrayRC elements
are of a different size than Array? Iconographically:

  sizeof(Array<Type>) != sizeof(ArrayRC<Type>)

It's hard to argue that the compiler is doing the "wrong" thing -- I'm
sure Dr. Dave will be happy to give an hour talk on this, as a warm-up
presentation for the ANSI cmte, complete with propositional logic statements
to the effect (for those of us who survived 242A, you know what I mean.
And I have only the warmest regards for this prof, so there! :-)

You might try this (although I've not tried this at home, YMMV):

// Specialization for Array<Type>

template <class Type>
void swap( Array<Type> &A, int i, int j )
{
  Type tmp = A[i]; A[i] = A[j]; A[j] = tmp;
}

// Specialization for ArrayRC<Type>

template <class Type>
void swap( ArrayRC<Type> &A, int i, int j )
{
  Type tmp = A[i]; A[i] = A[j]; A[j] = tmp;
}

You might also be far more compact, at the risk of misuse of your
class has no operator[] implemented:

// One size fits all swap, if you have an operator[]:

template <class SubscriptedType>
void swap( SubscriptedType &A, int i, int j)
{
  SubscriptedType tmp = A[i]; A[i] = A[j]; A[j] = tmp;
}

ObFollowup:
  Of course, if you really want a challenge in templates, but don't try
  it with the GNU compiler unless you've got the blecherously hacked code
  in libg++, read the STL code. I suggest the code from HP, personally.
  It shows you why you need the extra specializations.

-scottm
Technical Details
Message-ID: <42862c$kha@delphi.cs.ucla.edu>
Path: arc1-ca.pugleaf.net!pelican.cs.ucla.edu!scottm
References: <425hh1$joo@delphi.cs.ucla.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!pelican.cs.ucla.edu!scottm
From: scottm@pelican.cs.ucla.edu (Scott Michel)
Newsgroups: grads,gnu.g++.help,comp.lang.c++
Subject: Re: Why doesn't this simple program compile?
Followup-To: grads,gnu.g++.help,comp.lang.c++
Date: 1 Sep 1995 23:47:24 GMT
Organization: UCLA Computer Science Dept.
Message-ID: <42862c$kha@delphi.cs.ucla.edu>
References: <425hh1$joo@delphi.cs.ucla.edu>
NNTP-Posting-Host: pelican.cs.ucla.edu
X-Newsreader: TIN [version 1.2 PL2]