Skip to content

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

Closed
wants to merge 1 commit into from

Conversation

rtischer8277
Copy link
Contributor

No description provided.

@ghost
Copy link

ghost commented May 31, 2022

CLA assistant check
All CLA requirements met.

@PRMerger5
Copy link
Contributor

@rtischer8277 : Thanks for your contribution! The author(s) have been notified to review your proposed change.

@ShannonLeavitt
Copy link
Contributor

@TylerMSFT – Can you review the proposed changes? IMPORTANT: When the changes are ready for publication, add a #sign-off comment to signal that the PR is ready for the review team to merge.

#label:"aq-pr-triaged"

@PRMerger7 PRMerger7 added the aq-pr-triaged Tracking label for the PR review team label May 31, 2022
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>
Copy link
Collaborator

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:
Copy link
Collaborator

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)

Copy link
Member

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;
Copy link
Collaborator

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.

Copy link
Collaborator

@TylerMSFT TylerMSFT left a 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
Copy link
Collaborator

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

Copy link
Member

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.

Copy link
Member

@StephanTLavavej StephanTLavavej left a 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:
Copy link
Member

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
Copy link
Member

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:
Copy link
Member

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.

@rtischer8277
Copy link
Contributor Author

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!=.

@TylerMSFT
Copy link
Collaborator

I opened github issue MicrosoftDocs/cpp-docs-pr#5329 to add the suggested note.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants