Skip to content

Commit dfd9351

Browse files
author
mikeblome
committed
more updates in progress on modern cpp topic
1 parent f02531f commit dfd9351

1 file changed

Lines changed: 35 additions & 59 deletions

File tree

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

Lines changed: 35 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -8,84 +8,63 @@ ms.assetid: 1cb1b849-ed9c-4721-a972-fd8f3dab42e2
88

99
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.
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 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.
1212

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++:
1414

15-
Modern C++ emphasizes:
15+
## Stack-based scope for smaller objects
1616

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
1818

19-
- Auto type inference instead of explicit type names.
19+
## Smart pointers for heap-allocated objects
2020

21-
- Smart pointers instead of raw pointers.
21+
## Auto type inference instead of explicit type names
2222

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
2424

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
2626

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).
2828

29-
- Exceptions, to report and handle error conditions.
29+
## Uniform initialization
3030

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
3232

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.
3434

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
3636

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).
3838

39-
```cpp
40-
#include <vector>
39+
## Standard Library algorithms
4140

42-
void f()
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
4742

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
5244

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
6246

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.
6748

68-
Here's how the same thing is accomplished in modern C++:
49+
## Exceptions
6950

70-
```cpp
71-
#include <memory>
72-
#include <vector>
7351

74-
void f()
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)-
7967

80-
for( auto& s : v )
81-
{
82-
if( s && *s == *p )
83-
{
84-
cout << *s << " is a match\n";
85-
}
86-
}
87-
}
88-
```
8968

9069
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.
9170

@@ -96,9 +75,6 @@ If you're coming to C++ from another language, especially from a managed languag
9675

9776
- [Uniform Initialization and Delegating Constructors](../cpp/uniform-initialization-and-delegating-constructors.md)
9877

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)
10278

10379
- [Smart Pointers](../cpp/smart-pointers-modern-cpp.md)
10480

0 commit comments

Comments
 (0)