Most important things about C++ templates… lesson learned

前端 未结 12 1467
暗喜
暗喜 2021-01-30 18:52

What are most important things you know about templates: hidden features, common mistakes, best and most useful practices, tips...common mistakes/oversight/assumptions

12条回答
  •  滥情空心
    2021-01-30 19:25

    Here are some rules:

    1. Don't write any templates unless you're writing a very, very generic library (STL and Boost are two prominent examples).
    2. Don't instantiate any non-trivial template too many times. Instantiating huge template classes is especially overkill. You should consider using inheritance and polymorphism (the simple way, I mean, using virtual functions).
    3. If you're writing any templates, knowing when to use const, mutable and volatile will save users of the template both compile and execution time.
    4. If you're instantiating any templates, use a good compiler.

提交回复
热议问题