Re: static polymorphism --- How it actually Happens ?
Hi James,
On 4 juin, 10:35, James Kanze <james.ka...@gmail.com> wrote:
On Jun 3, 5:39 pm, Jeff Schwab <j...@schwabcenter.com> wrote:
Pallav singh wrote:
Can anyone explain How it actually Happens ?
polymorphic behaviour needed is invariant and can be
determined at compile time. Then the Curiously Recurring
Template Pattern (CRTP) can be used to achieve static
polymorphism, which is an imitation of polymorphism in
programming code but which is resolved at compile time and
thus does away with run-time virtual-table lookups.
Static polymorphism isn't an imitation of anything. In
canonical OO-speak, "polymorphism" is the ability of different
objects to respond to the same message in different ways.
Polymorphism isn't restricted to OO; it's a well established
concept, first described, I think, in a paper by Christopher
Strachey in 1967. The "reference", as far as I know, is "On
Understanding Types, Data Abstraction, and Polymorphism", by
Cardelli and Wegner
(http://lucacardelli.name/Papers/OnUnderstanding.A4.pdf). The
concept includes such things as function overloading and
implicit conversions, as well as parametric polymorphism
except that in this paper, parametric polymorphism doesn't refer to C+
+ templates (i.e. type injection) but to Java generics (i.e. type
erasure) which are fundamentally different. A refinement/extension is
needed to be clear/complete.
and
inclusion. Roughly speaking, a function or operator is
polymorphic if it can be invoked on different types. (Thus, in
C, the + operator is polymorphic, since I can add integers, or
floating point values.)
In languages that are not restricted to OO, "static"
polymorphism means roughly "the ability of fixed syntax to
mean different things, depending on context."
Formally, static doesn't mean anything when applied to
polymorphism. Informally, it is usually used to mean that the
polymorphism is somehow resolved at compile time, as opposed to
runtime. But even this doesn't mean much when more dynamic
languages are involved, like Lisp. Or for that matter, even in
C++: if I provide a single class interface (no virtual
functions), with several different implementations, in different
DLL's, and choose which DLL to load at runtime, is that static
polymorphism, or dynamic?
It's not polymorphic at all because it's exclusive, that is only one
class implementation (one morph) is available at a time in the
program.
This kind of polymorphism is "static" in the sense that the
mapping is implemented at compile-time. The traditional
example is overloaded functions:
int square(int const i) { return i * i; }
double square(double const d) { return d * d; }
the const qualifier can be removed.
a+, ld.