You are actually attempting the impossible.
The very heart of the matter is simple: virtual
and template
do not mix well.
template
is about compile-time code generation. You can think of it as some kind of type-aware macros + a few sprinkled tricks for meta programming.
virtual
is about runtime decision, and this require some work.
virtual
is usually implemented using a virtual tables (think of a table which lists the methods). The number of methods need be known at compile time and is defined in the base class.
However, with your requirement, we would need a virtual table of infinite size, containing methods for types we haven't seen yet and that will only be defined in the years to come... it's unfortunately impossible.
And if it were possible ?
Well, it just would not make sense. What happens when I call Foo2
with an int
? It's not meant for it! Therefore it breaks the principle that Foo2
implements all the methods from IFoo
.
So, it would be better if you stated the real problem, this way we could help you at a design level rather than at a technical level :)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…