-
Notifications
You must be signed in to change notification settings - Fork 967
C++20 example was needed #3940
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
C++20 example was needed #3940
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,35 @@ class vector | |
|
||
The C++ Standard Library provides a default implementation for an allocator. In C++11 and later, the default allocator is updated to expose a smaller interface; the new allocator is called a *minimal allocator*. In particular, the minimal allocator's `construct()` member supports move semantics, which can greatly improve performance. In most cases, this default allocator should be sufficient. In C++11 all the Standard Library types and functions that take an allocator type parameter support the minimal allocator interface, including `std::function`, `shared_ptr, allocate_shared()`, and `basic_string`. For more information on the default allocator, see [`allocator` Class](allocator-class.md). | ||
|
||
## Writing Your Own Allocator (C++20) | ||
Several Allocator member functions have been removed: **`address`**, **`max_size`**, **`construct`** and **`destroy`**. **`allocate(n)`** creates array space of type T, but does not construct the objects. Possible exceptions are not handled in the minimal example below: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to be specific about when they were removed. I believe they were removed in C++20. I see them in C++17 (project settings > General > C++ Language standard) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They were removed from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This has always been the case; it is not new to C++20. |
||
|
||
```cpp | ||
include <iostream> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo - missing # |
||
#include <memory> | ||
|
||
int main() | ||
{ | ||
// allocator for integer values | ||
std::allocator<int> myAllocator; | ||
|
||
// allocate space for five ints | ||
int* arr = myAllocator.allocate( 5 ); | ||
|
||
// construct and assign ints to elements 0 and 3 | ||
arr[ 0 ] = 100; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. our style is to not have spaces around array index, and to put the * with the variable, e.g. int *arr. And we might as well name it what it is - myArray, or something. |
||
arr[ 3 ] = 10; | ||
|
||
std::cout << arr[ 0 ] << std::endl; | ||
std::cout << arr[ 3 ] << std::endl; | ||
|
||
// destruct elements 0 and 3 and deallocate space for the five ints | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment is confusing to me (since ints aren't destructed). Maybe a more general statement that says // call destructors on the elements and deallocate the space There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
myAllocator.deallocate( arr, 5 ); | ||
|
||
return 0; | ||
} | ||
``` | ||
|
||
## Writing Your Own Allocator (C++11) | ||
|
||
The default allocator uses **`new`** and **`delete`** to allocate and deallocate memory. If you want to use a different method of memory allocation, such as using shared memory, then you must create your own allocator. If you are targeting C++11 and you need to write a new custom allocator, make it a minimal allocator if possible. Even if you have already implemented an old-style allocator, consider modifying it to be a *minimal allocator* in order to take advantage of the more efficient `construct()` method that will be provided for you automatically. | ||
|
Uh oh!
There was an error while loading. Please reload this page.