- Hands-On High Performance Programming with Qt 5
- Marek Krajewski
- 312字
- 2021-07-02 13:54:02
Other improvements
We will mention some more new features of modern C++, which can be used to improve performance, but for a detailed discussion, please refer to your favorite C++11/17 book:
- Guaranteed copy elision: As we said, RVO/NRVO was only an optional compiler optimization in C++89. C++11 renamed it to copy elision and extended its function to catching exceptions. C++17 introduced guaranteed copy elision for the case of unnamed RVO as a mandatory optimization technique.
- Better allocators: We already highlighted the fact that, in old C++, the STL allocator classes were little loved and little used. C++11 simplified the creation of custom allocators, with the intention that they should be more widely used to customize the memory management of STL container classes. Additionally, a new type of allocators was introduced, namely polymorphic allocators, which are non-templated and can be used to change allocators for collections without changing collections' effective types, hence plugging in an allocator that's better suitable in a specific context. Admittedly, it is an advanced and relatively new technique.
- In-place construction: The STL containers got new method overloads using move semantics, but in order to spare the move, a kind of new type of reference passing was introduced—that is, passing only the object's constructor parameters and creating the object in-place inside of the SLT container. This was made possible using new language features such as perfect forwarding and variadic templates. An example of such a method is std::vector<T>::emplace_back().
- noexcept modifier: This is also an optimization that's performed for STL containers. When you define the constructors of your data type as noexcept, STL can optimize certain operations, that is, use move semantics for the copying of data.
- std::string_view: This class was introduced in C++17 and it allows you to pass "slices" of a string, hence alleviating the need to construct substring objects in some contexts.