Introduction to C++
C++ is a general-purpose, statically typed programming language that supports multiple paradigms. It is known for its efficiency, scalability, and flexibility, and is commonly used in fields such as system development, game development, embedded systems, and high-performance computing. C++ was initially developed by Bjarne Stroustrup in the 1980s, building upon and enhancing the foundation of the C language.
Advantages: It runs very fast (although that doesn’t mean you can code quickly!) Disadvantages: It’s challenging to learn! This is due to its rich language features, complex syntax and concepts, the need for a deep understanding of low-level details, and extensive standard libraries and tools.
Reference: Tiobe Index
Summary of C++
Why summarize? Because C++ continues to evolve, making it more modern, secure, and user-friendly, with significant changes occurring almost every 3 to 4 years. Below is a summary of the important milestones in modern C++ (post-C++11, including C++11, C++14, C++17, C++20), totaling 30 items.
30 items of modern C++
- Automatic type inference (
auto
keyword): Allows the compiler to deduce the type of a variable based on the type of the initialization expression.(𝘊++ 11) - Range-based for loop: Allows convenient iteration over elements in a container using syntax like
for (auto& x : vec)
.(𝘊++ 11) - Move semantics: Introduces rvalue references (
&&
) andmove
constructors to efficiently move temporary objects, improving performance.(𝘊++ 11) - New container types: Introduces new standard containers like
std::array
,std::unordered_map
, etc.(𝘊++ 11) - Lambda expressions: Similar to anonymous functions, can be used where function objects are needed.(𝘊++ 11)
- Null pointer constant (
nullptr
): Used to represent null pointers, replacing the traditionalNULL
macro.(𝘊++ 11) - Smart pointers:
std::unique_ptr
andstd::shared_ptr
provide safer and more convenient memory management.(𝘊++ 11) - Enhanced support for multithreading: Introduces multithreading-related libraries like
std::thread
,std::mutex
, etc.(𝘊++ 11) constexpr
: Used for compile-time evaluation of constant expressions, improving performance.(𝘊++ 11)static_assert
: Used for static assertions at compile-time to check code consistency.(𝘊++ 11)- Binary literals: Allows the use of
0b
or0B
as prefixes to represent binary numbers.(𝘊++ 14) - Generic lambda expressions: Lambda expressions can use
auto
in parameter lists, facilitating the creation of generic function objects.(𝘊++ 14) - Return type deduction: Allows the return type of functions to be deduced using
auto
.(𝘊++ 14) - Initialization capture: In lambda expressions, variables can be captured by initialization (e.g.,
[x = std::move(x)]
).(𝘊++ 14) Constexpr functions: Allows the use of
if
statements and local variables in constexpr functions.(𝘊++ 14)- Fold expressions: Introduces new syntax for simplifying traversal and operations on parameter packs.(𝘊++ 17)
- Enhance shared_ptr: shared_ptr supports for creating dynamic arrays(𝘊++ 17)
- Initialization in if statements: Allows direct initialization of variables in if and switch statements.(𝘊++ 17)
- Structured bindings: Allows unpacking of tuples or other types of objects into multiple variables.(𝘊++ 17)
- constexpr if: Introduces new syntax for compile-time selection based on conditions.(𝘊++ 17)
- New features for aggregate classes: Relaxes restrictions on aggregate classes, allowing base classes, additional non-static data members, etc.(𝘊++ 17)
reduce: perform reduction operations on a range of elements(𝘊++ 17)
- Concepts: Introduces new syntax for defining constraints on template formal parameters to improve the readability and error messages of template code.(𝘊++ 20)
- Operations on ranges: Introduces new libraries for defining and manipulating the concept of ranges.(𝘊++ 20)
- Coroutines: Introduces new syntax and libraries for supporting coroutines, making asynchronous programming more convenient.(𝘊++ 20)
- Range-based for Loop Enhancements: supports initialization statements and initializers in the range-based for loop(𝘊++ 20)
- Modules: Introduces new syntax and standard libraries for defining and using modules to address issues with traditional header file inclusion mechanisms.(𝘊++ 20)
- Designated initializer: Explicitly specify how each member of a structure or union is initialized when using an initializer list.(𝘊++ 20)
- three-way comparison operator: It simplifies and unifies comparison operations between objects, allowing the generation of all comparison operators at once.(𝘊++ 20)
- likely and unlikely Attributes: provide the compiler with additional information to better improve performance.(𝘊++ 20)