-
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
Conversation
@rtischer8277 : Thanks for your contribution! The author(s) have been notified to review your proposed change. |
@TylerMSFT – Can you review the proposed changes? IMPORTANT: When the changes are ready for publication, add a #label:"aq-pr-triaged" |
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: | ||
|
||
```cpp | ||
include <iostream> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo - missing #
@@ -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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
They were removed from std::allocator
, but that is not especially relevant when talking about how to write a user-defined allocator.
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 comment
The 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much for adding to the docs!
I left a couple comments.
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
deallocate()
doesn't call destructors at all (for any types); it expects the elements to have already been destroyed. In the case of int
at runtime it doesn't really matter, which is why this example works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is unclear to me whether this PR should be merged at all. Perhaps the "Writing Your Own Allocator (C++11)" section should be slightly amended to talk about C++20 changes.
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
allocate(n)
creates array space of type T, but does not construct the objects.
This has always been the case; it is not new to C++20.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
deallocate()
doesn't call destructors at all (for any types); it expects the elements to have already been destroyed. In the case of int
at runtime it doesn't really matter, which is why this example works.
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
They were removed from std::allocator
, but that is not especially relevant when talking about how to write a user-defined allocator.
I agree with the comments. My example code does not show how a user would write their own allocator. But the Mallocator example does and it doesn't use any of the C++20 deprecated methods address, max_size, construct or destroy. This means that my submitted code and text should be replaced with a simple note, as suggested, in the Writing An Allocator (C++03) section to the effect that, as of C++20 these 4 methods have been removed and that it is unnecessary to provide the operator!=. |
I opened github issue MicrosoftDocs/cpp-docs-pr#5329 to add the suggested note. |
No description provided.