Re: generic programming is very powerful. c should support it
- From: dj3vande@xxxxxxxxxxxxxxxxxxxxxxxxxxxx (Dave Vandervies)
- Date: Fri, 28 Jul 2006 20:40:43 +0000 (UTC)
In article <4itkivF58uj2U1@xxxxxxxxxxxxxx>,
Ian Collins <ian-news@xxxxxxxxxxx> wrote:
Dave Vandervies wrote:
In article <ea7qle$3p6$1@xxxxxxxxxxxxxxxx>,
Christopher Benson-Manica <ataru@xxxxxxxxxxxxxxxxxxx> wrote:
Templates may work fine for functions, but you open the functionHow do you propose to make use of templates without introducing the
notion of a class?
By writing type-generic code once and using it type-safely for different
types, which works just as well for functions as it does for classes.
If you make the template parameters part of the function's name, you
don't even have to introduce overloading, though that does require the
template parameters at every use of the template instead of having the
template function be an overloaded function named by the template name.
--------
/*Generic template for scalar types*/
template<typename T>
int less<T>(const T *left, const T *right)
{ return *left < *right; }
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
--
Dave Vandervies dj3vande@xxxxxxxxxxxxxxxxxxx
Hide the distillery behind the desk, run a beige tube to your wireless keyb.
Flip up the capslock key to insert the drinking straw.
--Chris Hacking in the scary devil monastery
.
- Follow-Ups:
- Re: generic programming is very powerful. c should support it
- From: Ian Collins
- Re: generic programming is very powerful. c should support it
- References:
- Re: generic programming is very powerful. c should support it
- From: Dave Vandervies
- Re: generic programming is very powerful. c should support it
- From: Ian Collins
- Re: generic programming is very powerful. c should support it
- Prev by Date: Re: The Wikipedia article on C and C++ operators
- Next by Date: Re: The C language in the planet Mars
- Previous by thread: Re: generic programming is very powerful. c should support it
- Next by thread: Re: generic programming is very powerful. c should support it
- Index(es):
Relevant Pages
|