combining polymorphism and templates

Ted Byers
Thu Sep 27 16:39:00 GMT 2007

--- Matt Brown <> wrote:

> [snip]
> The output I get is:
> int: int functor
> float: generic functor
> base: base functor
> derived: derived functor
> derived2: generic functor
> derived3: generic functor
> Which is what I want/expect except for the last line
> of output.  I want:
> derived3: derived functor
Why would you expect that?  That would be inconsistent
with the output you get on the second last line.  You
did not provide a specialization of your functor for
any derived class other than "derived".  I don't know
what the standard has to say about this, but unless it
requires templates to process arbitrary class
hierarchies to select a specialization for the most
derived base for the template class argument, if there
is one, I would expect it to look only at the type
argument and its visible interface.  If my expectation
is right, then the output you are getting is correct.

Your output is what I would have expected.  If you
want static polymorphism that takes advantage of any
dynamic polymorphism you have defined, I'd suggest
modifying your code to make use of a virtual function
table.  That is, have a specialization of your
functor, defined for pointers to the base class,
invoke a virtual function in the class hierarchy
through the base class pointer.  That is the only way
I can see of generating output similar to what you say
you want.



More information about the Gcc-help mailing list