C++ As a classic programming language , Since 1980s, it has been widely used in system development and high performance computing . In recent years, with the rise of various programming languages and paradigms ,C++ Gradually fade out of people's vision . But as a language that is still evolving ,C++ In recent years, it has developed rapidly , It has the characteristics of modern language , And there are a lot of existing and ongoing new developments .
classical C++
As C The superset of language , One side ,C++ Integrated C Advantages in system programming , It can control every memory accurately bit; On the other hand , Provides a wealth of abstract mechanisms and programming paradigms , The introduction of object-oriented 、 Generic programming and functional programming . Because of that ,C++ Have and C Comparable runtime performance , On the other hand , It also simplifies C Language brings the difficulty of domain modeling . But because C++ The overall design combines a variety of styles , It is almost equivalent to a huge system of several small languages , It also makes C++ The overall ease of learning and ease of use of some poor . meanwhile , Because the standard library update can't keep up with the demand , In such as Concurrency/Network The software design aspect of application layer is gradually Java Wait for the successor to replace . and , each C++ Manufacturers do not fully refer to compiler implementation ISO standard , It also causes a lot of cross platform portability and compatibility problems .
modern C++
C++ Several explorations and transformations have been made in recent years , Let the whole language become more modern .
Resource management
RAII(Resource Aquiration is Initialization, Resource acquisition is initialization ) As C++ One of the characteristics of , It is widely applied to C++ In the program .RAII Control resources through the lifetime of heap objects ( Including heap memory 、 File handle 、 Network connection, etc. ) Life cycle of , Make resource management more automated , It also avoids the runtime burden of introducing garbage collection . But this model has a very important problem , When you need to share resources , More extra work needs to be done to check and synchronize .
As a more modern way of resource management ,C++11 Two kinds of intelligent pointer are introduced in ,std::shared_ptr and std::unique_ptr. The former has a thread safe reference count , The latter is through ownership (owenrship) Transfer to control the life cycle of resources .C++11 Right value reference and mobile semantics are also introduced in , To avoid unnecessary duplication in the process of resource transfer .
And Rust Life cycle in (Lifetime) And ownership (Ownership) The concept of is similar to ,C++ Of std::unique_ptr Transfer the resource held by the user to the target of the assignment every time the value is passed , At the same time, combined with mobile semantics , Further optimize the assignment process .
Lambda
Functor As C++ STL An important component of , It's also C++ One of the features that are used a lot in . One Functor In fact, it is a heavy load operator() Instance object of the class of , This kind of object coordinates with C++ Template behavior , It can be simply called as a function , So it's called Functor( Functor ). however , because C++ Support for anonymous classes and inner classes is not good enough , Use Functor You have to design ahead of time . On the one hand, it is inconvenient to use , On the other hand , The separation of definition and use , The organization and understanding of the code also causes some difficulties .
First ,lambda As Functor substitute , Solved the problem that can't be defined and used immediately . coordination STL Containers and algorithms in ,lambda Also can be C++ The functional style of the play to the extreme . secondly , Out of C++ A consistent consideration of performance and abstraction , Introduced lambda capture The concept of , Enables the life cycle of an object to be bound to lambda expression , You can build closure objects (closure). in addition ,C++14 Added to generic lambda, Enhanced lambda The type derivation algorithm of , Without loss of type safety features , Let combinatorial programming (Combinator–based Programming) Easier to implement .
Concurrent
stay C++ The beginning of design , Concurrency is not taken into account as a core language feature . also , Thread and other concurrency models are implemented on different platforms , It is also difficult to build a unified concurrency model .
C++11 Redesigned C++ Memory model , On the basis of maintaining the original compatibility, concurrent content is added . At the same time, the standard library has also added threads (<thread>)、 Semaphore (<condition_variable>)、 The mutex (<mutex>) And atomic manipulation (<atomic>) The content such as . At the same time, it also encapsulates future/promise Patterns and async Wait for the operation .
Metaprogramming
C++ It provides good support for metaprogramming . Templates as one of the main components , Provides compilation time numerical calculation and type calculation . But on the one hand, the use of templates slows down the compilation speed , On the other hand , When using templates , Very difficult to debug and debug , It's deterring a lot of people , Even for template based STL Components also have a sense of fear .
C++11 Meta programming support has been enhanced in . The first is to put type traits Introduce as a standard library , Can provide a set of intuitive constraints to the template , Let the type be C++ The first kind of value in (first-class value) There is ; in addition constexpr It simplifies the value operation at compile time , With the user-defined literal amount (user-defined literals) And variable parameter templates (varadic template/parameter pack) Other characteristics , Give Way C++ It's easier to define the interior DSL.
Bright Future
As a classic programming language ,C++ It is still constantly updated . stay C++20 in , With these important characteristics :
A richer Library of standards :C++ Chinese vs File System、Network And other important components have been standardized support ,
Module TS: Modular proposals , Used in place of inherited from C Language header file , simplify C++ The compiler model and module dependencies of ,
Concepts TS: Used to enhance type constraints and type derivation , It also simplifies the use of templates ,
Reflection TS: Provides support for compile time static reflection , Simplify and enhance type traits, Provides richer metaprogramming capabilities .
summary
You can see C++ Development has been in the forefront of the times . One side , More components suitable for application and system development have been added , On the other hand , Simplify Abstract complexity by extending language features . As such a programming language with both new features and historical responsibilities , Enough to foresee the breadth of its application ; Again , More system level open source projects , image Mesos etc. , Also choose C++ The main language of programming is . There are enough reasons for us to believe that ,C++ It's a new life .
See here, are you right about “C++” I have a little new understanding ~
If you like this article , Move your little finger , Pay attention to it ~
Programming learning books :
Programming learning video :