What C++ idioms should C++ programmers use? [closed]

懵懂的女人 提交于 2019-11-28 15:09:00

Here is one list. If I had to pick a couple I might go with the Curiously Recurring Template Pattern or Virtual Contstructors.

By far the single most important "pattern" to learn and know that's (nearly) unique to C++ is RAII (Resource Acquisition Is Initialization).

Edit: (To answer extra question edited into the question). You use RAII primarily to (semi-)automate resource management. The most obvious use is freeing resources owned by objects when the owning objects go out of scope, such as freeing memory or closing files.

Dmitry

PIMPL, aka P ointer to IMPL ementation ?

Template metaprogramming. It's great because it's basically compile-time duck typing, so you get most of the flexibility of duck typing with the speed of static typing.

If you want to get the most out of the STL then iterators and functors/function objects are essential idioms. The use of iterators also implicitly relies on the 'half-open range' idiom too.

Exploiting Strong Typing and const correctness is also extremely helpful.

I also prohibit default copy constructors and assignment operators. I actually go beyond that, but those are the most common. I think life would be easier if they were not implicit.

RAII, COW, pimpl, law of demeter (not sure if can be classified as idiom), type traits and policies. (COW and law of demeter are not limited to C++ though)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!