Question:
Is there a way to call the "base" template function from a specialized template function in C ++, how can a child class access the parent versions of virtual methods when overriding them? (NOTE: I suspect the answer is no, but would like to be wrong)
Context:
I often find myself a specialist in template functions only because a special case needs additional preliminary or subsequent processing, and not because the "guts" of the code have changed.
To give a far-fetched example:
With inheritance, you can do the following:
struct base { virtual void go() { printf("%p", this); } }; struct foo : base { virtual void go() { printf("this foo lives at "); base::go(); } };
... and calling foo :: go () will print "this foo lives in <address>"
With templates:
template <typename T> void go(T const &t) { printf("%p\n", &t); } template <> void go(foo const &f) { printf("this foo lives at "); ??? how to access "base" template ??? }
You can get around the problem in an ugly way by decomposing the mess of small helper functions and specializing in them, rather than the function that you really need:
template <typename T> void _go_pre(T const &t) { } template <typename T> void _go_post(T const &t) { } template <typename T> void go(T const &t) { _go_pre(t); printf("%p\n", &t); _go_post(t); } template<> void _go_pre(foo const &t) { printf("this foo lives at "); }
... but this significantly clogs the code, because now the base template needs to foresee all the ways that its "child" specialization can redefine, and most types will use several, if any, of these hooks. The mess becomes unreadable and unnoticeable quite quickly because the cause of these hooks is unknown at the place where they are identified, and you need to check the various combinations of hooks used / not used.
All this is exactly the same as the problems you would have with redefining virtual methods in a world where the child class could not access the original version provided by the parent class.