STL and Boost of C++

Reprinted from: https://www.douban.com/note/499300037/

STL and Boost of C++

In the past year, I interviewed dozens of C++ candidates on the phone. The usual warm-up question is "Which components of STL have you used in your work? Which components of Boost have you used?". Most of the answers obtained are concentrated in vector, map and shared_ptr. If the other party is a student at school, I usually ask about the internal implementation of vector or map, the complexity of various operations, and the possible scenarios of iterator failure. If you are an experienced programmer, I will also ask about the thread safety of shared_ptr, the consequences of circular references and how to avoid them, and the role of weak_ptr. If these are all answered well, you can further ask how to implement thread-safe reference counting, how to customize delete actions, and so on. These questions allow me to quickly identify the C++ level of the other party.
The reason I asked about Boost during the interview is because many of the components can indeed be used to write maintainable product code. Boost contains nearly a hundred program libraries, many of which have engineering practical value. Everyone has different tastes and technical backgrounds, and different choices for Boost. As far as my personal experience is concerned, first of all, you can use absolutely harmless libraries, such as noncopyable, scoped_ptr, static_assert, etc. These libraries are relatively simple to learn and use, and easy to get started. Secondly, some functions are not difficult to implement by themselves. It just happens that Boost provides ready-made codes, so you might as well use them, such as date_time and circular_buffer. Then, in new projects, more modern methods can be considered for message passing and resource management, such as using function/bind in some cases instead of virtual functions as the callback interface of the library, using shared_ptr to implement thread-safe object callbacks, etc. . These two will affect the design ideas and styles of the entire program, and need to be considered comprehensively. If smart pointers are used correctly, the delete statement is generally not needed in modern C++ programs. Finally, be wary of some poorly performing libraries, such as lexical_cast. In short, on the basis that everyone in the project team can understand and use, the off-the-shelf Boost components are appropriately introduced to reduce duplication of work and improve productivity.
Boost is a treasure house, in which there are both code that can be used directly and design ideas worth learning. Let me give you an example: the regular expression library regex handles thread-safety.
The early RegEx class was not thread-safe, it put "regular expression" and "matching action" into one class. Due to variable data, RegEx objects cannot be used across threads. Today's regex clearly distinguishes immutable and mutable data. The former can be safely shared across threads, while the latter cannot. For example, the regular expression itself (basic_regex) and the result of a match (match_results) are immutable; and the matching action itself (match_regex) involves state updates and is changeable, so it is encapsulated with a reentrant function. Let these data leak to other threads. It is precisely because of this reasonable distinction that regex does not need to be locked during normal use.
Donald Knuth expressed such a view in the book "Coders at Work": If the programmer's job is to fiddle with parameters to call ready-made libraries, and don't know how these libraries are implemented, then this career is not much fun. Words. In other words, although we emphasize not to reinvent the wheel at work, as a qualified programmer, you should have the ability to make your own wheels. It's not impossible, it's not for it.
A major feature of the C/C++ language is that its standard library can be implemented in the language itself. The strlen, strcpy, and strcmp functions of the C standard library are good subjects for teaching and practice. The complex, string, and vector of the C++ standard library are excellent examples of class, resource management, and template programming. After in-depth understanding of the implementation of STL, the use of STL is natural and can automatically avoid some errors and inefficient usage.
The same is true for Boost. In order to eliminate the doubts when using it, and in order to use it more smoothly, sometimes we need to properly understand its internal implementation, and even write a simplified version for comparison and verification. However, because Boost code uses advanced syntax and techniques that are not common in daily application development, and uses a large number of preprocessing macros to cross multiple platforms and compilers, reading Boost source code is not easy and requires a lot of effort. On the other hand, if you are obsessed with these interesting low-level details and forget what problem you originally intended to solve, you will probably lose everything.
Many libraries in Boost are designed according to the paradigm of generic programming. For those familiar with object-oriented programming, they may face a change of thinking. For example, you have to be familiar with the terminology of generic programming, such as concept, model, refinement, to easily understand the description of various locks in the Boost.Threads document. I think this is not a big problem for people who are familiar with STL design concepts.
In some areas, Boost is not the only choice, nor is it necessarily the best choice. For example, to generate formulaic source code, I would prefer to write a small code generation program in a scripting language instead of Boost.Preprocessor; to embed a domain-specific language in a C++ program, I would prefer to use Lua or other language interpreters, and Don't use Boost.Proto; if you want to use C++ programs to parse context-free grammar, I would prefer to use ANTLR to define lexical and grammatical rules and generate a parser instead of Boost.Spirit. In short, when using Boost, you have to be calm and don't try to transform the C++ language. It is the key to give full play to the part of the function that helps to increase productivity and let the project benefit from it.
To learn Boost, in addition to reading the documents, examples and source code on its official website, it is best to have a relatively comprehensive Chinese book on hand to read at any time. For developers who do not speak English, this is even more fortunate. The "Boost Library Complete Development Guide" in your hand is a good user guide and reference manual. The author introduces most of the common content of Boost from the shallower to the deeper, allowing readers to quickly understand Boost and find the part they need. After I got the manuscript of this book, I read it carefully and carefully. Overall, the author is very skilled and pragmatic. I have a good understanding and application of C++ and Boost. I have learned nothing from this book. Little new knowledge. For this reason, I am happy to recommend this reliable book to developers who want to learn the Boost library.
It is important to know that "there is no donation". As a modern C++ programmer, the energy invested in Boost will surely be rewarded.

Guess you like

Origin blog.csdn.net/sinat_16643223/article/details/115260462