D
Dave Vandervies
Dave said:Templates may work fine for functions, but you open the function
overload/name mangling can of worms.
There's no overloading happening here (less<int> and less<double> are
two different functions with two different names; the only difference
from writing less_int and less_double is that the programmer only has
to write the template code once and the compiler handles rewriting it
for every type used), and name mangling can be as simple as replacing
the punctuators with something that the linker will handle gracefully
(say, less$int and less$double, if the linker will treat $ as part of
an identifier).
You would also end up with all of
the messy name resolution rules from C++.
Not having objects or inheritance makes template name resolution rules
a lot simpler; you could probably do it with just these:
If there is a fully specialized template for these parameters, use it.
(Having more than one fully specialized template for the same parameters
would be easily checkable and should probably be a constraint violation)
If there is no fully specialized template, and exactly one partially
specialized one, that matches these parameters
(ex. bogosort<double,pred> or bogosort<T,my_compare_function>), use
that partially specialized one.
If there are more than one partially specialized templates that match
these parameters, the behavior is undefined. (A smart compiler could
detect the condition and complain; a less clever compiler could make
an arbitrary choice about which one to use.)
If there are no fully or partially specialized templates that match the
parameters but there is a fully generic one, use it. (Parameters are
either type names or constant expressions of a specific type, so
there's no ambiguity about whether it matches.)
Attempting to use a template with no matching definition is a constraint
violation.
I don't know the C++ rules, but I'd expect most of the messiness comes
from interactions with overloading and inheritance.
There's nothing in templates that requires objects and inheritance or
overloading, and leaving those out makes specifying the templates a lot
easier (because it reduces the amount of non-obvious interaction rules
that need to be gotten right)
dave