Templates in FPL?
D. Tweed
tweed@compsci.bristol.ac.uk
Wed, 23 May 2001 10:44:04 +0100 (BST)
On 22 May 2001, Carl R. Witty wrote:
> "D. Tweed" <tweed@cs.bris.ac.uk> writes:
>
> > In my experience the C++ idiom `you only pay for what you use' (==>
> > templates are essentially type-checked macros) and the fact most compilers
> > are evolved from C compilers makes working with templates a real pain in
> > practice.
>
> I'm not sure what you mean by type-checked here. Templates are not
> type-checked at definition time, but are type-checked when they are
> used; the same is true of ordinary macros.
I was thinking in terms of (to take a really simple example)
template<class T>
void
initialiseArray(T** arr,const T& elt,int bnd)
{
for(int i=0;i<bnd;++i){
arr[i]=elt.realValue(); // <-----------------
}
}
If I try and use intialiseArray(<obj of type foo>,<obj of type bar>), with
the template function I get the error when passing in the parameters; with
a macro the type error would appear at the indicated line, and indeed if
by pure chance bar just happened to have a member function called
realValue then I wouldn't get one at all. In my view it's reasonable
to say that template functions are type-checked in essentially the same
way as general functions, giving type errors in terms of the source
code that you're using, whereas there's no way to ignore the fact
macros dump a lot of code at the calling point in your program (mapping
certain identifiers to local identifiers) and then type-checking in
the context of the calling point. The above code is a really contrived
example, but I do genuinely find it useful that template type-error
messages are essentially normal function type-error messages.
[As an aside to Marcin, one of my vague plans (once I've been viva'd,
etc) is to have a go at writing some Haskell code that attempts to try and
simplify g++ template type errors, e.g., taking in a nasty screen long
error message and saying
`This looks like a list<a> ---- list<a>* mistake'
Like everything I plan to do it may not materialise though.]
___cheers,_dave________________________________________________________
www.cs.bris.ac.uk/~tweed/pi.htm|tweed's law: however many computers
email: tweed@cs.bris.ac.uk | you have, half your time is spent
work tel: (0117) 954-5250 | waiting for compilations to finish.