2

In the wikipedia article on Parametric Polymorphism, is the following commentary on C++. Can anyone explain what it's referring to when talking about "ad hoc aspects"? (I think it needs a citation).

Some implementations of type polymorphism are superficially similar to parametric polymorphism while also introducing ad hoc aspects. One example is C++ template specialization.

From reading The Four Polymorphisms in C++, it would imply the wikipedia article is talking about "ad-hoc polymorphism" aka overloading. Is that correct?

  • The article you link is incorrect in equating ad hoc polymorphism with overloading. Overloading is a kind of ad hoc polymorphism, but so is any other variant where different implementations are used based on different types, which includes templates. – Jules Mar 3 '16 at 10:38
3

No. It has nothing to do with overloading. The "ad-hoc aspects" aren't ad-hoc additions, they are differences that make templates something quite different from parametric polymorphism. In other words, as the Wikipedia article was indicating, templates in C++ are something entirely distinct from parametric polymorphism.

Parametric polymorphism means that a function's type is parameterized by another type and thus the function will work for all potentially provided types. For this to work, the function can assume nothing about the type except what is true for all types. The simplest example is the type of the identity function:

template <typename T> T identity(T t);

If this was actually a parametrically polymorphic type, we'd know that aside from doing some side-effects, the only thing identity could do is return its argument, i.e. the only (returning) implementation would be:

template <typename T> T identity(T t) { doStuff(); return t; }

But this is not at all enforced by C++. In particular, the following is a completely valid implementation as far as C++ is concerned:

template <typename T> T identity(T t) { return 0; }

Of course this will only work for types T for which the expression 0 makes sense. You can use this template function throughout your code with no problems. You will only find out there is an issue if you ever instantiate T to a type for which 0 makes no sense. If C++ templates were parametric polymorphism the last implementation of identity would be rejected as ill-typed, regardless of whether or how it was used.

C++ templates are what they say on the tin; they are code templates that can be, essentially syntactically, instantiated, the result then type-checked. They are not types themselves.

  • 1
    "They are not types themselves." – Type constructors in parametric polymorphism also aren't types, they are type constructors. Types have kind *, whereas type constructors have kind * → * or something more complex. – Jörg W Mittag Mar 3 '16 at 8:03
  • 1
    "they are code templates that can be, essentially syntactically, instantiated, the result then type-checked." – Exactly, they have different "specialized" instantiations for each type. Which is pretty much the definition of "ad-hoc polymorphism". – Jörg W Mittag Mar 3 '16 at 8:04
  • 1
    @JörgWMittag A type like forall a. a → a does have kind *. I didn't say anything at all about type constructors and they are an orthogonal feature to parametric polymorphism. C and C++ do have type constructors such as the array type constructor. Function templates aren't type constructors either; type constructors of kind * → * don't have values like identity. – Derek Elkins Mar 3 '16 at 14:49
0

I think the Wikipedia article is right. Template specialization allows ad-hoc behavior.

You can think of template specializations of function templates as function overloads. However, the concept of overloads does not exist for classes.

When it comes to classes, template specialization are exactly that - specializations of a generic template that allows implementation of ad-hoc behavior.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.