0
votes

After reading Polymorphism in C++ and What is polymorphism, what is it for, and how is it used? I noticed many answers had differences in what they listed as methods of achieving polymorphism and I wanted to make a list for myself that was more comprehensive.

Mechanisms for Achieving Polymorphism

  1. Overloading (functions or operators)
  2. Overriding functions through inheritance (without virtual)
  3. Virtual functions
  4. Templates
  5. Preprocessing (for ex. using #define, this was taken from the answer in the first link)
  6. Standard conversions (ex. implicit conversions)

What am I missing or leaving out? Is this list comprehensive enough?

After reading https://catonmat.net/cpp-polymorphism I was also interested in categorizing these mechanisms.

Ad hoc (also known as runtime polymorphism):

  • Virtual
  • Overriding

Parametric (also known as compile-time polymorphism):

  • Templates

Subtyping (also known as overloading)

  • Overloading
  • Overloading

Coersion (also known as casting)

  • Standard conversions

Are these categorizations correct?


For reference: (Taken from https://en.wikipedia.org/wiki/Polymorphism_(computer_science))

The most commonly recognised major classes of polymorphism are:

Ad hoc polymorphism: defines a common interface for an arbitrary set of individually specified types.

Parametric polymorphism: when one or more types are not specified by name but by abstract symbols that can represent any type.

Subtyping (also called subtype polymorphism or inclusion polymorphism): when a name denotes instances of many different classes related by some common superclass.

1

1 Answers

0
votes

No.

None of those really cover what std function, vatious function views, variant, or what std any do.

Std function and similar may be implemented using some of those, but calling it any of those is misleading. Interface wise, there is no exposed inheritance. And operator() is not a template, but behaves polymorphically.

If you want good answers to this you'll probably have to delve into category or type theory. Which is above my pay grade.