Alfonso Morra wrote:
I have written a class, and several of it's methods are function
templates. When I compiled the code, I realized that I had made some
typos in the code - but the compiler seemed to skip right over these
obvious errors.
Templates don't generate code until they are instantiated. It is a
feature, and a very nice one, for that matter. It helps generic
programming, because you can define functions that may not apply to all
specializations. For example:
template <class Container>
bool IsMember(const Container& items, const Container::referece_type value)
{
return items.find(value) != items.end();
}
This function is always there, but it can only be called if the
container has a .find() member function, or else it will fail at compile
time. On the other hand, as long as the function is not called, it won't
issue any errors.
Note that the compiler doesn't know anything about Container until it
actually sees a specific instantiation. There's nothing in the above
template code that advises that Container has a .find method that takes
a single input, and an .end() method that takes no args, nor is it clear
that the two are comparable via operator!=. Only when you actually
create an instance of a template class or call a template function will
the compiler do a full syntax check and validation.
Tom