Most important things about C++ templates… lesson learned

前端 未结 12 1724
暗喜
暗喜 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:16

    I tend to use templates quite a lot to avoid duplication of code, and to increase safety through compile checks.

    In general, it helps to think while typing about what the compiler is going to do, and how the code will be generated for each type.

    Being very iterative in development and building the template complexity little by little has helped me avoiding sinking in compile error messages. Don't forget to keep a simple (or mock) instantiation of the template somewhere, otherwise you might have some nasty surprises when you instantiate a monster template for the first time.

    Finally, when there is no way out, get to read these compile error messages! They might look quite scary at first, but they really are helpful. Maybe at first extracting the first one, copying it in a text editor and making it look pretty will help getting used to them, and it quickly becomes second nature to read through.

提交回复
热议问题