T
Tom Craig
A few times recently I've wanted to use a virtual member template,
like this:
struct A {
template<class T> virtual void f();
};
Unfortunately, the C++ standard forbids this. I'm a bit confused about
this: Why should virtual member templates be forbidden?
The only (well, "only") reason I've come across is that, when
compiling the class, the compiler doesn't know how many entries to put
in the vtbl. Couldn't this be worked around by adding an extra level
of indirection? In particular, the entry for f in A's vtbl would index
into a map from types to function addresses. When the linker combines
the objects, it *does* know how many entries are required (and what
they are), so it can resolve the calls - either by keeping the extra
level of indirection in at run-time or by expanding the class's vtbl
to the correct size.
Or have I got the wrong end of the stick? I'm interested in knowing
because (a) I've recently been developing an increasing interest in
the way compilers and linkers work, and (b) virtual member templates
would be *sooo* very useful.
like this:
struct A {
template<class T> virtual void f();
};
Unfortunately, the C++ standard forbids this. I'm a bit confused about
this: Why should virtual member templates be forbidden?
The only (well, "only") reason I've come across is that, when
compiling the class, the compiler doesn't know how many entries to put
in the vtbl. Couldn't this be worked around by adding an extra level
of indirection? In particular, the entry for f in A's vtbl would index
into a map from types to function addresses. When the linker combines
the objects, it *does* know how many entries are required (and what
they are), so it can resolve the calls - either by keeping the extra
level of indirection in at run-time or by expanding the class's vtbl
to the correct size.
Or have I got the wrong end of the stick? I'm interested in knowing
because (a) I've recently been developing an increasing interest in
the way compilers and linkers work, and (b) virtual member templates
would be *sooo* very useful.