Basic C++ Idioms / Techniques

前端 未结 6 1342
闹比i
闹比i 2021-02-02 01:21

Note: marked as community wiki.

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

Besides:

  • using the STL
  • implementing RAII
相关标签:
6条回答
  • 2021-02-02 01:40

    I think this should cover it:

    More C++ Idioms - Wikibooks

    0 讨论(0)
  • 2021-02-02 01:41

    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
    0 讨论(0)
  • 2021-02-02 01:50

    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.

    0 讨论(0)
  • 2021-02-02 01:58

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

    0 讨论(0)
  • 2021-02-02 02:00
    • OO Design
    • Types of exception safety guarantees (which is what most design patterns/idioms are based on).
    • When to use which standard containers
    • Boost
    0 讨论(0)
  • 2021-02-02 02:04

    (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.

    0 讨论(0)
提交回复
热议问题