Two weeks ago, an ISO C ++ standard committee met in Jacksonville. Today I want to present a short summary and write about the revolutionary decision taken at the Jacksonville meeting. For more information, I recommend reading the article C ++ will no longer have pointers . The Language Standardization Committee decided that the pointers will be declared obsolete in C ++ 20 and are more likely to be removed from C ++ 23.
Frankly, what seems revolutionary is just the last step of a long evolution.
Pointers exist in C ++ from the beginning. We got them from C. From the very beginning of C ++ development, there has always been a tendency to make pointer management more secure without significant losses.
In C ++ 98, we got std::auto_ptr
to express exclusive ownership. But std::auto_ptr
had a big flaw. When you copy std::auto_ptr
, ownership of the resource is transferred to the copy. Copy looked like moving. The image below shows the unpleasant behavior of std::auto_ptr
.
It was very bad, leading to a lot of serious bugs. Therefore, we obtained std::unique_ptr
in C ++ 11, and declared std::auto_ptr
obsolete in C ++ 11, and finally removed from C ++ 17. Additionally, we got std::shared_ptr
and std::weak_ptr
in C ++ 11 for managing ownership. You cannot copy, but you can move std::unique_ptr
, and if you copy or assign std::shared_ptr
, the counter of referencing pointers increases. Look here:
Starting from C ++ 11 C ++ has a multi-threaded library. This makes std::shared_ptr
quite complicated, because std::shared_ptr
by definition shared, but not thread safe. Only the control part with meters is thread safe, but not access to the address of the monitored resource. This means that changing the counter is an atomic operation, but you have no guarantee that the resource will be deleted exactly once. For this reason, we get atomic smart pointers in C ++: std::atomic_shared_ptr
and std::atmic_weak_ptr
. For details on the proposals of the standardization committee, read here: Atomic smart pointers .
Now we come to the more interesting parts of the future C ++ 20 and C ++ 23 standards. Pointers will be deprecated in C ++ 20 and removed from C ++ 23. Say three words: No New New (NNN).
But wait, what a C ++ dogma: Do not pay for what you do not need. How can we program without pointers? Just use std::unique_ptr
. From its design, std::unique_ptr
is as fast and economical as a regular pointer, and has a distinct advantage - automatic resource management.
Below is a simple performance test.
// all.cpp #include <chrono> #include <iostream> static const long long numInt= 100000000; int main(){ auto start = std::chrono::system_clock::now(); for ( long long i=0 ; i < numInt; ++i){ int* tmp(new int(i)); delete tmp; // std::shared_ptr<int> tmp(new int(i)); // std::shared_ptr<int> tmp(std::make_shared<int>(i)); // std::unique_ptr<int> tmp(new int(i)); // std::unique_ptr<int> tmp(std::make_unique<int>(i)); } std::chrono::duration<double> dur= std::chrono::system_clock::now() - start; std::cout << "time native: " << dur.count() << " seconds" << std::endl; }
This program allocates and frees memory for 100 million int
. I use pointers, std::shared_ptr
and std::unique_ptr
in two variations. I compile a program with and without maximum optimization in Linux and in Windows. Such numbers turn out:
Two variations of std::unique_ptr
on Linux and Windows show the same performance as regular pointers. For details on this test, refer to my previous article: Memory Consumption and Smart Pointer Performance .
Honestly, we use pointers and, in particular, ordinary pointers very often. The question, should you use a pointer, is this: Who is the owner? Fortunately, with the help of the code we can clearly express it.
std::shared_ptr
. I can explicitly delete my shared resource if no one else needs it.std::weak_ptr::lock
method.We will need to change only one of the six practices of using pointers and we are happy with the next step in developing C ++.
Source: https://habr.com/ru/post/352570/
All Articles