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
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions docs/standard-library/allocators.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

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.


```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 #

#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;
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.

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

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.
Expand Down