Basic C++ Idioms / Techniques

一曲冷凌霜 提交于 2019-12-20 10:40:10

问题


Note: marked as community wiki.

In recent days, I've realized how little I know about C++.

Besides:

  • using the STL
  • implementing RAII
  • implementing ref-counted smart pointers
  • writing my own policy-based template classes
  • overloading operators << for fun

What other techniques are must-know for a good C++ programmer?

Thanks!


回答1:


I think this should cover it:

More C++ Idioms - Wikibooks




回答2:


  • OO Design
  • Types of exception safety guarantees (which is what most design patterns/idioms are based on).
  • When to use which standard containers
  • Boost



回答3:


The first two are 'must know' for a good C++ programmer. 'Good C++ programmers' do not overload operators for fun.




回答4:


Basic:

  • RTTI
  • Virtual functions
  • shared_ptr etc
  • Templates
  • Virtual inheriting
  • Variadic macros

Also useful:

  • Attributes (it depends on your compiler)
  • Variadic templates
  • Variadic functions
  • Constexpr (sorting in compile time / calculating hash of strings etc... but the latter is related to the last section)
  • Lambdas

Useful for brainfucking or in special cases:

  • CRTP
  • SFINAE
  • inable_if (type traits)
  • Foreach macro
  • User-defined literals



回答5:


(hardly a must-know, but still useful) Writing domain-specific languages with operator overloading and template metaprogramming (see Boost.Spirit for a nice example) - but this is the kind of thing that makes shooting yourself in the foot easy, too.




回答6:


The way I used to improve my c++ is reading the source code of leveldb. Because leveldb is a product level code. So you can learn the cpp idiom and design pattern from a real product. Let me show you some example

Leveldb use the Pimpl idiom, almost in all of it's head file, such table.h table_build.h write_batch.h. You can learn from the code directly

Leveldb use many OO design pattern, such as build pattern, the table have the table_build class to build the table, the block have the block_build class to build the block

Leveldb also use the Iterator pattern, the iterator make us use leveldb more convenient.

So I think leveldb contain many idiom or design pattern that c++ engineer should know.



来源:https://stackoverflow.com/questions/2345177/basic-c-idioms-techniques

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