Thread View: grads
3 messages
3 total messages
Started by Ching-Tsun Chou
Thu, 31 Aug 1995 23:44
Why doesn't this simple program compile?
Author: Ching-Tsun Chou
Date: Thu, 31 Aug 1995 23:44
Date: Thu, 31 Aug 1995 23:44
78 lines
2322 bytes
2322 bytes
I'm reading S.B. Lippman's book "C++ Primer" (2nd ed.) and trying out
the example programs in it using gcc-2.7.0. According to Lippman's book,
the following simple program should compile, though when executed it should
fail at the line marked by "// ERROR". But I couldn't even compile it
using gcc-2.7.0. If the program is in a file "prog.cc", then compiling it
with "g++ prog.cc" generates the following error message:
prog.cc: In function `int main()':
prog.cc:50: type unification failed for function template
`template <class Type> void swap(Array<...> &, int, int)'
where line 50 is the line marked by "// ERROR". What is wrong?
I'd appreciate any comments!
- Ching Tsun
PS. Please send your replies to <chou@cs.ucla.edu>.
PPS. The same program without template did compile and produced the
expected results when executed.
================================= prog.cc =================================
#include <assert.h>
#include <iostream.h>
const int ArraySize = 12;
template <class Type>
class Array
{
public:
Array( int sz = ArraySize )
{ size = sz; ia = new Type[size]; }
~Array()
{ delete [] ia; }
Array& operator=( const Array& );
virtual Type& operator[]( int i )
{ return ia[i]; }
int getSize()
{ return size; }
protected:
int size;
Type *ia;
};
template <class Type>
class ArrayRC : public Array<Type>
{
public:
ArrayRC( int sz = ArraySize )
: Array<Type>( sz ) {} ;
Type& operator[]( int i )
{ assert( i >= 0 && i < size ); return ia[i]; }
};
template <class Type>
void swap( Array<Type> &A, int i, int j )
{
Type tmp = A[i]; A[i] = A[j]; A[j] = tmp;
}
main()
{
Array<int> ia1;
ArrayRC<int> ia2;
cout << "swap with Array ia1" << endl ;
swap( ia1, 1, ia1.getSize() );
cout << "swap with ArrayRC ia2" << endl ;
swap( ia2, 1, ia2.getSize() ); // ERROR
return 0;
}
Re: Why doesn't this simple program compile?
Author: Enno Sandner
Date: Fri, 01 Sep 1995 07:54
Date: Fri, 01 Sep 1995 07:54
54 lines
1995 bytes
1995 bytes
The program is correct according to the draft-standard.
But most compilers, including g++ doesn't support this
new style of template-argument matching, therefore you
are still restricted to a more or less exact-type matching.
So you'll have to use cast's to fix your problem.
regards
Enno
chou@cs.ucla.edu (Ching-Tsun Chou) writes:
>I'm reading S.B. Lippman's book "C++ Primer" (2nd ed.) and trying out
>the example programs in it using gcc-2.7.0. According to Lippman's book,
>the following simple program should compile, though when executed it should
>fail at the line marked by "// ERROR". But I couldn't even compile it
>using gcc-2.7.0. If the program is in a file "prog.cc", then compiling it
>with "g++ prog.cc" generates the following error message:
>
> prog.cc: In function `int main()':
> prog.cc:50: type unification failed for function template
> `template <class Type> void swap(Array<...> &, int, int)'
>
>
>template <class Type>
>class Array
>{
> ...
>};
>
>template <class Type>
>class ArrayRC : public Array<Type>
>{
> ...
>};
>
>template <class Type>
>void swap( Array<Type> &A, int i, int j )
>{
> ...
>}
>
>main()
>{
> Array<int> ia1;
> ArrayRC<int> ia2;
>
> cout << "swap with Array ia1" << endl ;
> swap( ia1, 1, ia1.getSize() );
>
> cout << "swap with ArrayRC ia2" << endl ;
> swap( ia2, 1, ia2.getSize() ); // ERROR
>
> return 0;
>}
Re: Why doesn't this simple program compile?
Author: Scott Michel
Date: Fri, 01 Sep 1995 23:47
Date: Fri, 01 Sep 1995 23:47
70 lines
2947 bytes
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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads