Re: static polymorphism --- How it actually Happens ?
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.
In languages that are not restricted to OO, "static" polymorphism means
roughly "the ability of fixed syntax to mean different things, depending
on context." 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; }
/* Function resolution depends on the type of n.
* Which function to call is determined at compile-time.
*/
square(n);
In statically typed languages that support operator overloading, viz.
C++, static polymorphism can be more subtle:
/* This syntax may or may not imply a function call.
* The determination is made at compile-time, though
* the operation may be performed at run-time.
*/
v += 5;