How to achieve “virtual template function” in C++

后端 未结 9 1664
囚心锁ツ
囚心锁ツ 2020-11-28 03:20

first off: I have read and I know now that a virtual template member function is not (yet?) possible in C++. A workaround would be to make the class a template and then use

9条回答
  •  暖寄归人
    2020-11-28 03:58

    In you scenario, you are trying to mix compile time polymorphism with runtime polymorphism, but it cannot be done in this "direction".

    Essential, your AMOUNT template argument represents an expected interface for the type to implement based on the union of all the operations each implementation of eat uses. If you where to create an abstract type that declared each of those operations making them virtual where needed, then you could call eat with different types (that derived from your AMOUNT interface). And it would behave as expected.

提交回复
热议问题