Re: generic programming is very powerful. c should support it



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:

How 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; }

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

--
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
.



Relevant Pages

  • Re: LoadString overwrite
    ... doing some special string replacements. ... In MFC 8.0 the CString class is a template class and I couldn't figure ... Overloading it, you get a C2084 (explicit specialization; ...
    (microsoft.public.vc.mfc)
  • Re: LoadString overwrite
    ... In MFC 8.0 the CString class is a template class and I couldn't figure ... Overloading it, you get a C2084 (explicit specialization; ... You might try suppressing the instantiation using the "extern template" ... play linker games to replace member functions. ...
    (microsoft.public.vc.mfc)
  • LoadString overwrite
    ... doing some special string replacements. ... In MFC 8.0 the CString class is a template class and I couldn't figure ... Overloading it, you get a C2084 (explicit specialization; ...
    (microsoft.public.vc.mfc)
  • Re: Help: How many explicit specializations required?
    ... > non template functions are overloadable ... specialization of the primary template. ... I think the above case is a partial ordering (overloading of the ... instantiation will be entered into the list of candidate functions. ...
    (comp.lang.cpp)