You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
10
10
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 new features that enable type-safe ways to reduce the need to C-style idioms.
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.
12
12
13
-
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 simple, safe, elegant, and as fast as ever.
13
+
The following sections provide an overview of the main features of modern C++:
14
14
15
-
Modern C++ emphasizes:
15
+
## Stack-based scope for smaller objects
16
16
17
-
- Stack-based scope with automatic memory management instead of heap or static global scope.
17
+
It has been said that the reason C++ has no garbage collection mechanism is because it doesn't produce that much "garbage", in other words, memory allocations on the heap that need to be deleted. Small objects can be allocated directly on the stack, where they will
18
18
19
-
- Auto type inference instead of explicit type names.
19
+
## Smart pointers for heap-allocated objects
20
20
21
-
- Smart pointers instead of raw pointers.
21
+
## Auto type inference instead of explicit type names
22
22
23
-
-`std::string` and `std::wstring` types (see [\<string>](../standard-library/string.md)) instead of raw `char[]` arrays.
23
+
for variables and function return types
24
24
25
-
-[C++ Standard Library](../standard-library/cpp-standard-library-header-files.md) containers like `vector`, `list`, and `map` instead of raw arrays or custom containers. See [\<vector>](../standard-library/vector.md), [\<list>](../standard-library/list.md), and [\<map>](../standard-library/map.md).
25
+
## Range-based for loops
26
26
27
-
-C++ Standard Library [algorithms](../standard-library/algorithm.md) instead of manually coded ones.
27
+
Use range-based for loops to write more robust loops that work with arrays, C++ Standard Library containers, and Windows Runtime collections in the form `for ( for-range-declaration : expression )`. This is part of the Core Language support. For more information, see [Range-based for Statement (C++)](../cpp/range-based-for-statement-cpp.md).
28
28
29
-
- Exceptions, to report and handle error conditions.
29
+
## Uniform initialization
30
30
31
-
- Lock-free inter-thread communication using C++ Standard Library `std::atomic<>` (see [\<atomic>](../standard-library/atomic.md)) instead of other inter-thread communication mechanisms.
31
+
## String classes for text data
32
32
33
-
- Inline [lambda expressions](../cpp/lambda-expressions-in-cpp.md) instead of small functions implemented separately.
33
+
`std::string`, `std::wstring` and [std:;string_view](../standard-library/string-view-class.md)types (see [\<string>](../standard-library/string.md)) instead of raw `char[]` arrays.
34
34
35
-
- Range-based for loops to write more robust loops that work with arrays, C++ Standard Library containers, and Windows Runtime collections in the form `for ( for-range-declaration : expression )`. This is part of the Core Language support. For more information, see [Range-based for Statement (C++)](../cpp/range-based-for-statement-cpp.md).
35
+
## std::vector and other Standard Library containers
36
36
37
-
The C++ language itself has also evolved. Compare the following code snippets. This one shows how things used to be in C++:
37
+
[C++ Standard Library](../standard-library/cpp-standard-library-header-files.md) containers like `vector`, `list`, and `map` instead of raw arrays or custom containers. See [\<vector>](../standard-library/vector.md), [\<list>](../standard-library/list.md), and [\<map>](../standard-library/map.md).
38
38
39
-
```cpp
40
-
#include<vector>
39
+
## Standard Library algorithms
41
40
42
-
voidf()
43
-
{
44
-
// Assume circle and shape are user-defined types
45
-
circle* p = new circle( 42 );
46
-
vector<shape*> v = load_shapes();
41
+
C++ Standard Library [algorithms](../standard-library/algorithm.md) instead of manually coded ones. parallel versions in C++17
47
42
48
-
for( vector<circle*>::iterator i = v.begin(); i != v.end(); ++i ) {
49
-
if( *i && **i == *p )
50
-
cout << **i << " is a match\n";
51
-
}
43
+
## Move semantics
52
44
53
-
// CAUTION: If v's pointers own the objects, then you
54
-
// must delete them all before v goes out of scope.
55
-
// If v's pointers do not own the objects, and you delete
56
-
// them here, any code that tries to dereference copies
57
-
// of the pointers will cause null pointer exceptions.
58
-
for( vector<circle*>::iterator i = v.begin();
59
-
i != v.end(); ++i ) {
60
-
delete *i; // not exception safe
61
-
}
45
+
## Lambda expressions
62
46
63
-
// Don't forget to delete this, too.
64
-
delete p;
65
-
} // end f()
66
-
```
47
+
Inline [lambda expressions](../cpp/lambda-expressions-in-cpp.md) instead of small functions implemented separately.
67
48
68
-
Here's how the same thing is accomplished in modern C++:
49
+
## Exceptions
69
50
70
-
```cpp
71
-
#include<memory>
72
-
#include<vector>
73
51
74
-
voidf()
75
-
{
76
-
// ...
77
-
auto p = make_shared<circle>( 42 );
78
-
vector<shared_ptr<shape>> v = load_shapes();
52
+
Exceptions, to report and handle error conditions.
53
+
54
+
## Resource management
55
+
56
+
-[Object Lifetime And Resource Management](../cpp/object-lifetime-and-resource-management-modern-cpp.md)
57
+
58
+
-[Objects Own Resources (RAII)](../cpp/objects-own-resources-raii.md)
59
+
60
+
## Lock-free inter-thread communication
61
+
62
+
using C++ Standard Library `std::atomic<>` (see [\<atomic>](../standard-library/atomic.md)) instead of other inter-thread communication mechanisms.
63
+
64
+
## - [Pimpl For Compile-Time Encapsulation](../cpp/pimpl-for-compile-time-encapsulation-modern-cpp.md)
65
+
66
+
## [Portability At ABI Boundaries](../cpp/portability-at-abi-boundaries-modern-cpp.md)-
79
67
80
-
for( auto& s : v )
81
-
{
82
-
if( s && *s == *p )
83
-
{
84
-
cout << *s << " is a match\n";
85
-
}
86
-
}
87
-
}
88
-
```
89
68
90
69
Modern C++ incorporates two kinds of polymorphism: compile-time, through templates, and run-time, through inheritance and virtualization. You can mix the two kinds of polymorphism to great effect. The C++ Standard Library template `shared_ptr` uses internal virtual methods to accomplish its apparently effortless type erasure. But don't over-use virtualization for polymorphism when a template is the better choice. Templates can be very powerful.
91
70
@@ -96,9 +75,6 @@ If you're coming to C++ from another language, especially from a managed languag
96
75
97
76
-[Uniform Initialization and Delegating Constructors](../cpp/uniform-initialization-and-delegating-constructors.md)
98
77
99
-
-[Object Lifetime And Resource Management](../cpp/object-lifetime-and-resource-management-modern-cpp.md)
100
-
101
-
-[Objects Own Resources (RAII)](../cpp/objects-own-resources-raii.md)
0 commit comments