Is it possible to write an agile Pimpl in c++?

后端 未结 4 1490
無奈伤痛
無奈伤痛 2020-12-03 16:14

I\'ve been playing with the Pimpl idiom and reaping all sorts of benefits from it. The only thing I haven\'t been too keen on is the feeling I get when I define the function

4条回答
  •  离开以前
    2020-12-03 16:44

    I'm just going to start by sumarizing to make sure I understand: You like the benefits of using pimpl, but dislike the amount of boilerplate code when adding or modifying functions?

    In a nutshell, there is no template magic you can use to eliminate this boilerplate, but there are things to consider here as well:

    • You write code only once but read it many times, and you have at your disposal a variety of copy-paste capabilities. Initially creating the function isn't the majority of the time you will spend on this class. Compiling and maintaining is where your time will be spent.
    • Be sure to keep the public class API as simple as possible. The fewer functions you have in the public API the less boilerplate you have to write. You can make as many functions as you like in the impl and y ou only have to modify them there.
    • If you find yourself changing the public class API many many times, you might wish to slightly adjust your design process. Spend ten more minutes up front looking at/writing down use cases and you may reduce your API changes by 90%.

提交回复
热议问题