When to prefer templated policy based design over non-templated inheritance based design

后端 未结 2 573
南笙
南笙 2021-02-14 10:44

I am trying to understand the real requirement of the usage of templates for policy based design. Going through the new templated designs in C++ I found that policy based class

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-14 11:18

    Both are valid ways of structuring, it actually depends on the requirements. E.g.

    Runtime vs compile time polymorphism.

    When do you want/can/have to achieve polymorphism ?

    Performance overhead of virtual calls

    Templates generate code that has no indirections

    The actual usage of the class.

    When you have to store heterogenous collections, a base class is needed, so you have to use inheritance.

    A very good book on policy-based design (a bit dated but good nevertheless) is Modern C++ Design

提交回复
热议问题