Getting rid of dynamic polymorphism and classes

Tommi tommitissari at hotmail.com
Sun Nov 11 23:20:16 PST 2012


I should point out, just so that no-one is missing the obvious
here, that you'd be using static polymorphism through the use of
template functions whenever it's possible, i.e. when you don't
need to store polymorphic types.

So, you'd do this:

template <typename S>
auto func(const S& shape)
-> typename std::enable_if<
      is_shape<S>::value
>::type
{
      // ...
}

You would NOT do this:

void func(const Shape& shape)
{
      // ...
}

The big question is then: is the code bloat introduced by the
massive use of template functions worth the speed gained through
static polymorphism (vs. dynamic). Whether it is or not, the main
gain for me is in de-coupling of types, which helps in writing
truly generic code.


More information about the Digitalmars-d mailing list