C ++ has a kind of duck typing of types defined by template parameters. We donβt know what the DUCK1 and DUCK2 types will be, but as long as they can quack() , it will compile and run:
template <class DUCK1, class DUCK2> void let_them_quack(DUCK1* donald, DUCK2* daisy){ donald->quack(); daisy->quack(); }
But it is a little inconvenient to write. When I am completely uninterested in what actual types DUCK1 and DUCK2 most likely want to fully use the idea of ββduck text input, then I would like to have something similar to something more than the above:
- I would like to omit the list of template options, which is repetitive and basically meaningless (just imagine what would happen if there are 7 ducks ...)
- I would like to make it somewhat more explicit that types are never used and that this only matters for the interface.
- I would like to have an annotation / interface check. Itβs somehow clear which interface is expected for the type. (This, however, contrasts slightly with duck typing.)
Does C ++ offer any functions to achieve one or more of the three ideas?
(I know that virtual inheritance is the method of choice in most cases for implementing such patterns, but this is specifically about the case of static polymorphism.)
c ++ c ++ 11 templates duck-typing
Michael
source share