Skip to content

Commit 5ce8206

Browse files
author
mikeblome
committed
proofread pass MicrosoftDocs#1
1 parent 440a0c7 commit 5ce8206

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

docs/cpp/welcome-back-to-cpp-modern-cpp.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ ms.assetid: 1cb1b849-ed9c-4721-a972-fd8f3dab42e2
66
---
77
# Welcome back to C++ (Modern C++)
88

9-
Over the past 25 years, C++ has been one of the most widely used programming languages in the world. Well-written C++ programs are fast and efficient. The language is more flexible than other languages because it enables you to access low-level hardware features to maximize speed and minimize memory requirements. You can use it to create a wide range of apps—from games, to high-performance scientific software, to device drivers, embedded programs, libraries and compilers for other programming languages, Windows client apps, and much more.
9+
Over the past 25 years, C++ has been one of the most widely used programming languages in the world. Well-written C++ programs are fast and efficient. The language is more flexible than other languages because it enables you to access low-level hardware features to maximize speed and minimize memory requirements. You can use it to create a wide range of apps—from games, to high-performance scientific software, device drivers, embedded programs, libraries and compilers for other programming languages, Windows client apps, and much more.
1010

11-
One of the original requirements for C++ was backward compatibility with the C language. As a result C++ has always permitted C-style programming with raw pointers, arrays, null-terminated character strings, custom data structures, and other features that may enable great performance but can also spawn bugs and complexity. The evolution of C++ has emphasized features that greatly reduce the need to C-style idioms. The old C-programming facilities are there when you need them, but with modern C++ code you should need them less and less. Modern C++ code is simpler, safer, more elegant, and still as fast as ever.
11+
One of the original requirements for C++ was backward compatibility with the C language. As a result C++ has always permitted C-style programming with raw pointers, arrays, null-terminated character strings, custom data structures, and other features that may enable great performance but can also spawn bugs and complexity. The evolution of C++ has emphasized features that greatly reduce the need to use C-style idioms. The old C-programming facilities are there when you need them, but with modern C++ code you should need them less and less. Modern C++ code is simpler, safer, more elegant, and still as fast as ever.
1212

1313
The following sections provide an overview of the main features of modern C++. Unless noted otherwise, the features listed here are available in C++11 and later. In the Microsoft C++ compiler, you can set the [/std](../build/reference/std-specify-language-standard-version.md) compiler option to specify which version of the standard to use for your project.
1414

15-
## Prevent memory leaks through RAII and smart pointers
15+
## RAII and smart pointers
1616

17-
One of the major classes of bugs in C-style programming is the *memory leak* due to failure to release memory or other resources. Modern C++ emphasizes the principle of *resource acquisition is initialization* which states that any resource (heap memory, file handles, sockets, and so on) should be *owned* by an object that creates, or receives, the newly-allocated resource in its constructor, and deletes in its destructor. By adhering to the principle of RAII, you guarantee that all resources will be properly returned to the operating system when the owning object goes out of scope. To support easy adoption of RAII principles, the C++ Standard Library provides three smart pointer types: [std::unique_ptr](../standard-library/unique-ptr-class.md), [std::shared_ptr](../standard-library/shared-ptr-class.md), and [std::weak_ptr](../standard-library/weak-ptr-class.md). A smart pointer handles the allocation and deletion of the memory it owns. The following example shows a class with an array member that is allocated on the heap in the call to `make_unique()`. The calls to **new** and **delete** are encapsulated by the `unique_ptr` class. When a `widget` object goes out of scope, the unique_ptr destructor will be invoked and it will release the memory that was allocated for the array.
17+
One of the major classes of bugs in C-style programming is the *memory leak* due to failure to call **delete** for memory that was allocated with **new**. Modern C++ emphasizes the principle of *resource acquisition is initialization* which states that any resource (heap memory, file handles, sockets, and so on) should be *owned* by an object that creates, or receives, the newly-allocated resource in its constructor, and deletes it in its destructor. By adhering to the principle of RAII, you guarantee that all resources will be properly returned to the operating system when the owning object goes out of scope. To support easy adoption of RAII principles, the C++ Standard Library provides three smart pointer types: [std::unique_ptr](../standard-library/unique-ptr-class.md), [std::shared_ptr](../standard-library/shared-ptr-class.md), and [std::weak_ptr](../standard-library/weak-ptr-class.md). A smart pointer handles the allocation and deletion of the memory it owns. The following example shows a class with an array member that is allocated on the heap in the call to `make_unique()`. The calls to **new** and **delete** are encapsulated by the `unique_ptr` class. When a `widget` object goes out of scope, the unique_ptr destructor will be invoked and it will release the memory that was allocated for the array.
1818

1919
```cpp
2020
#include <memory>
@@ -39,11 +39,11 @@ void functionUsingWidget() {
3939

4040
Whenever possible, use a smart pointer when allocating heap memory. If you must use the new and delete operators explicitly, follow the principle of RAII. For more information, see [Object lifetime and resource management (RAII)](object-lifetime-and-resource-management-modern-cpp.md).
4141

42-
## Use std::string and std::string_view instead of C-style char arrays
42+
## std::string and std::string_view
4343

4444
C-style strings are another major source of bugs. By using [std::string and std::wstring](../standard-library/basic-string-class.md) you can eliminate virtually all the errors associated with C-style strings, and gain the benefit of member functions for searching, appending, prepending, and so on. Both are highly optimized for speed. When passing a string to a function that requires only read-only access, in (C++17) you can use [std::string_view](../standard-library/basic-string-view-class.md) for even greater performance benefit.
4545

46-
## Use std::vector and other Standard Library containers whenever possible
46+
## std::vector and other Standard Library containers
4747

4848
The Standard Library containers all follow the principle of RAII, provide iterators for safe traversal of elements, are highly optimized for performance and have been thoroughly tested for correctness. By using these containers whenever possible, you eliminate the potential for bugs or inefficiencies that might be introduced in custom data structures. By default, use [vector](../standard-library/vector-class.md) as the preferred sequential container in C++. This is equivalent to `List<T>` in .NET languages.
4949

@@ -70,15 +70,17 @@ When performance optimization is needed, consider using:
7070

7171
Don’t use C-style arrays. For older APIs that need direct access to the data, use accessor methods such as `f(vec.data(), vec.size());` instead. For more information about containers, see [C++ Standard Library Containers](../standard-library/stl-containers.md).
7272

73-
## Prefer Standard Library algorithms
73+
## Standard Library algorithms
7474

7575
Before you assume that you need to write a custom algorithm for your program, first review the C++ Standard Library [algorithms](../standard-library/algorithm.md). The Standard Library contains an ever-growing assortment of algorithms for many common operations such as searching, sorting, filtering, and randomizing. The math library is extensive. Starting in C++17, parallel versions of many algorithms are provided.
7676

7777
Here are some important examples:
7878

79-
- **for_each**, which is the default traversal algorithm. (Also **transform** for not-in-place semantics.)
79+
- **for_each**, the default traversal algorithm (along with range-based for loops).
8080

81-
- **find_if**, which is the default search algorithm.
81+
- **transform**, for not-in-place modification of container elements
82+
83+
- **find_if**, the default search algorithm.
8284

8385
- **sort**, **lower_bound**, and the other default sorting and searching algorithms.
8486

@@ -93,7 +95,7 @@ sort( v.begin(), v.end(), comp );
9395
auto i = lower_bound( v.begin(), v.end(), comp );
9496
```
9597
96-
### Use auto instead of explicit type names
98+
## auto instead of explicit type names
9799
98100
C++11 introduced the [auto](auto-cpp.md) keyword for use in variable, function, and template declarations. **auto** tells the compiler to deduce the type of the object so that you do not have to type it explicitly. **auto** is especially useful when the deduced type is a nested template:
99101
@@ -102,7 +104,7 @@ map<int,list<string>>::iterator i = m.begin(); // C-style
102104
auto i = m.begin(); // modern C++
103105
```
104106

105-
## Use range-based for loops to eliminate indexing errors
107+
## Range-based for loops
106108

107109
C-style iteration over arrays and containers is prone to indexing errors and is also tedious to type. To eliminate these errors, and make your code more readable, use range-based for loops with Standard Library containers as well as raw arrays. For more information, see [Range-based for statement](../cpp/range-based-for-statement-cpp.md).
108110

@@ -128,7 +130,7 @@ int main()
128130
}
129131
```
130132

131-
### Use constexpr expressions instead of macros
133+
## constexpr expressions instead of macros
132134

133135
Macros in C and C++ are tokens that are processed by the preprocessor prior to compilation. Each instance of a macro token is replaced with its defined value or expression before the file is compiled. Macros are commonly used in C-style programming to define compile-time constant values. However, macros are error-prone and difficult to debug. In modern C++, you should prefer [constexpr](constexpr-cpp.md) variables for compile-time constants:
134136

@@ -174,11 +176,11 @@ int main()
174176

175177
For more information, see [Brace initialization](initializing-classes-and-structs-without-constructors-cpp.md).
176178

177-
## Move semantics to avoid unnecessary copies
179+
## Move semantics
178180

179181
Modern C++ provides *move semantics* which make it possible to eliminate unnecessary memory copies which in earlier versions of the language were unavoidable in certain situations. A *move* operation transfers ownership of a resource from one object to the next without making a copy. When implementing a class that owns a resource such as heap memory, file handles, and so on, you can define a *move constructor* and *move assignment operator* for it. The compiler will choose these special members during overload resolution in situations where a copy is not needed. The Standard Library container types invoke the move constructor on objects if one is defined. For more information, see [Move Constructors and Move Assignment Operators (C++)](move-constructors-and-move-assignment-operators-cpp.md).
180182

181-
## Lambda expressions instead of function pointers
183+
## Lambda expressions
182184

183185
In C-style programming, a function can be passed to another function by means of a *function pointer*. Function pointers are inconvenient to maintain and understand because the function they refer to may be defined elsewhere in the source code, far away from the point at which it is being invoked. Also, they are not type-safe. Modern C++ provides function objects, classes that override the [()](function-call-operator-parens.md) operator which enables them to be called like a function. The most convenient way to create function objects is with inline [lambda expressions](../cpp/lambda-expressions-in-cpp.md). The following example shows how to use a lambda expression to pass a function object, that the `for_each` function will invoke on each element in the vector:
184186

@@ -195,11 +197,11 @@ The lambda expression `[=](int i) { return i > x && i < y; }` can be read as "fu
195197
196198
As a general rule, modern C++ emphasizes exceptions rather than error codes as the best way to report and handle error conditions. For more information, see [Modern C++ best practices for exceptions and error handling](errors-and-exception-handling-modern-cpp.md).
197199
198-
## std::atomic for lock-free inter-thread communication
200+
## std::atomic
199201
200202
Use the C++ Standard Library [std::atomic](../standard-library/atomic-structure.md) struct and related types for inter-thread communication mechanisms.
201203
202-
## std::variant instead of unions (C++17)
204+
## std::variant (C++17)
203205
204206
Unions are commonly used in C-style programming to conserve memory by enabling members of different types to occupy the same memory location. However, unions are not type-safe and are prone to programming errors. C++17 introduces the [std::variant](../standard-library/variant-class.md) class as a more robust and safe alternative to unions. The [std::visit](../standard-library/variant-functions.md#visit) function can be used to access the members of a `variant` type in a type-safe manner.
205207

0 commit comments

Comments
 (0)