-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Uses-Allocator and guaranteed copy elision For piecewise construction #1668
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
70747d8
Implement P0475R1 and P0591R4 with test coverage
AdamBucior 73740fa
clang-format
AdamBucior 50f41a0
Fixes
AdamBucior 8aa9b42
More fixes
AdamBucior 0ea01b9
clang-format
AdamBucior ceb840c
Even more fixes
AdamBucior 61d618d
Skip some tests and code review
AdamBucior 7abf4fc
Update assertion comment
AdamBucior e33ba06
Simplify polymorphic_allocator::construct
AdamBucior 68ee695
Update expected_results.txt
AdamBucior e2b6aa2
Skip more libcxx tests
AdamBucior 923255b
Code review
AdamBucior c5d953c
Forgot to commit test changes
AdamBucior de905a9
clang-format
AdamBucior 60e01f9
Code review feedback (comments, test variables).
StephanTLavavej 5accd7c
Merge branch 'main' into uses-allocator
StephanTLavavej File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
tests/std/tests/P0475R1_P0591R4_uses_allocator_construction/env.lst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| RUNALL_INCLUDE ..\usual_latest_matrix.lst |
181 changes: 181 additions & 0 deletions
181
tests/std/tests/P0475R1_P0591R4_uses_allocator_construction/test.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include <cassert> | ||
| #include <memory> | ||
| #include <scoped_allocator> | ||
| #include <tuple> | ||
| #include <type_traits> | ||
| #include <utility> | ||
|
|
||
| using namespace std; | ||
|
|
||
| void test_P0475R1() { | ||
| struct DoNotCopy { | ||
| DoNotCopy() = default; | ||
| DoNotCopy(const DoNotCopy&) { | ||
| assert(false); | ||
| } | ||
| }; | ||
|
|
||
| struct X { | ||
| using allocator_type = allocator<int>; | ||
| X(DoNotCopy&&, const allocator_type&) {} | ||
| }; | ||
|
|
||
| scoped_allocator_adaptor<allocator<pair<X, int>>> alloc; | ||
AdamBucior marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| auto ptr = alloc.allocate(1); | ||
| alloc.construct(ptr, piecewise_construct, tuple<DoNotCopy>{}, make_tuple(1)); | ||
| alloc.destroy(ptr); | ||
| alloc.deallocate(ptr, 1); | ||
| } | ||
|
|
||
| constexpr bool test_P0591R4() { | ||
| allocator<int> alloc; | ||
| int i = 5; | ||
| pair p(i, i); | ||
|
|
||
| struct AllocatorArgConstructible { | ||
| using allocator_type = allocator<int>; | ||
|
|
||
| constexpr AllocatorArgConstructible(allocator_arg_t, const allocator<int>&, int y) : x(y) {} | ||
|
|
||
| int x; | ||
| }; | ||
|
|
||
| struct AllocatorConstructible { | ||
| using allocator_type = allocator<int>; | ||
|
|
||
| constexpr AllocatorConstructible(int y, const allocator<int>&) : x(y) {} | ||
|
|
||
| int x; | ||
| }; | ||
|
|
||
| struct OnlyAllocatorArgConstructible { | ||
| using allocator_type = allocator<int>; | ||
|
|
||
| constexpr OnlyAllocatorArgConstructible(allocator_arg_t, const allocator<int>&) {} | ||
| }; | ||
|
|
||
| struct OnlyAllocatorConstructible { | ||
| using allocator_type = allocator<int>; | ||
|
|
||
| constexpr OnlyAllocatorConstructible(const allocator<int>&) {} | ||
| }; | ||
|
|
||
| struct DefaultConstructible { | ||
| constexpr DefaultConstructible() {} | ||
| }; | ||
|
|
||
| using AllocatorArgConstructArgs = tuple<allocator_arg_t, const allocator<int>&, int&>; | ||
| using AllocatorConstructArgs = tuple<int&, const allocator<int>&>; | ||
| using ConstAllocatorArgConstructArgs = tuple<allocator_arg_t, const allocator<int>&, const int&>; | ||
| using ConstAllocatorConstructArgs = tuple<const int&, const allocator<int>&>; | ||
| using MovedAllocatorArgConstructArgs = tuple<allocator_arg_t, const allocator<int>&, int&&>; | ||
| using MovedAllocatorConstructArgs = tuple<int&&, const allocator<int>&>; | ||
| using OnlyAllocatorArgConstructArgs = tuple<allocator_arg_t, const allocator<int>&>; | ||
| using OnlyAllocatorConstructArgs = tuple<const allocator<int>&>; | ||
| using DefaultConstructArgs = tuple<>; | ||
|
|
||
| { // non-pair overload | ||
| auto tuple1 = uses_allocator_construction_args<int>(alloc, i); | ||
| static_assert(is_same_v<decltype(tuple1), tuple<int&>>); | ||
|
|
||
| auto tuple2 = uses_allocator_construction_args<AllocatorArgConstructible>(alloc, i); | ||
| static_assert(is_same_v<decltype(tuple2), AllocatorArgConstructArgs>); | ||
|
|
||
| auto tuple3 = uses_allocator_construction_args<AllocatorConstructible>(alloc, i); | ||
| static_assert(is_same_v<decltype(tuple3), AllocatorConstructArgs>); | ||
| } | ||
|
|
||
| { // pair(piecewise_construct_t, tuple, tuple) overload | ||
| auto tuple4 = uses_allocator_construction_args<pair<int, OnlyAllocatorArgConstructible>>( | ||
| alloc, piecewise_construct, forward_as_tuple(i), forward_as_tuple()); | ||
| static_assert( | ||
| is_same_v<decltype(tuple4), tuple<piecewise_construct_t, tuple<int&>, OnlyAllocatorArgConstructArgs>>); | ||
|
|
||
| auto tuple5 = uses_allocator_construction_args<pair<AllocatorConstructible, DefaultConstructible>>( | ||
| alloc, piecewise_construct, forward_as_tuple(i), forward_as_tuple()); | ||
| static_assert( | ||
| is_same_v<decltype(tuple5), tuple<piecewise_construct_t, AllocatorConstructArgs, DefaultConstructArgs>>); | ||
| } | ||
|
|
||
| { // pair() overload | ||
| auto tuple6 = | ||
| uses_allocator_construction_args<pair<DefaultConstructible, OnlyAllocatorArgConstructible>>(alloc); | ||
| static_assert(is_same_v<decltype(tuple6), | ||
| tuple<piecewise_construct_t, DefaultConstructArgs, OnlyAllocatorArgConstructArgs>>); | ||
|
|
||
| auto tuple7 = uses_allocator_construction_args<pair<OnlyAllocatorConstructible, DefaultConstructible>>(alloc); | ||
| static_assert(is_same_v<decltype(tuple7), | ||
| tuple<piecewise_construct_t, OnlyAllocatorConstructArgs, DefaultConstructArgs>>); | ||
| } | ||
|
|
||
| { // pair(first, second) overload | ||
| auto tuple8 = uses_allocator_construction_args<pair<int, AllocatorArgConstructible>>(alloc, i, i); | ||
| static_assert( | ||
| is_same_v<decltype(tuple8), tuple<piecewise_construct_t, tuple<int&>, AllocatorArgConstructArgs>>); | ||
|
|
||
| auto tuple9 = uses_allocator_construction_args<pair<AllocatorConstructible, int>>(alloc, i, i); | ||
| static_assert(is_same_v<decltype(tuple9), tuple<piecewise_construct_t, AllocatorConstructArgs, tuple<int&>>>); | ||
| } | ||
|
|
||
| { // pair(const pair&) overload | ||
| auto tuple10 = uses_allocator_construction_args<pair<int, AllocatorArgConstructible>>(alloc, p); | ||
| static_assert(is_same_v<decltype(tuple10), | ||
| tuple<piecewise_construct_t, tuple<const int&>, ConstAllocatorArgConstructArgs>>); | ||
|
|
||
| auto tuple11 = uses_allocator_construction_args<pair<AllocatorConstructible, int>>(alloc, p); | ||
| static_assert( | ||
| is_same_v<decltype(tuple11), tuple<piecewise_construct_t, ConstAllocatorConstructArgs, tuple<const int&>>>); | ||
| } | ||
|
|
||
| { // pair(pair&&) overload | ||
| auto tuple12 = uses_allocator_construction_args<pair<int, AllocatorArgConstructible>>(alloc, move(p)); | ||
| static_assert( | ||
| is_same_v<decltype(tuple12), tuple<piecewise_construct_t, tuple<int&&>, MovedAllocatorArgConstructArgs>>); | ||
|
|
||
| auto tuple13 = uses_allocator_construction_args<pair<AllocatorConstructible, int>>(alloc, move(p)); | ||
| static_assert( | ||
| is_same_v<decltype(tuple13), tuple<piecewise_construct_t, MovedAllocatorConstructArgs, tuple<int&&>>>); | ||
| } | ||
|
|
||
| { | ||
| auto obj1 = make_obj_using_allocator<AllocatorArgConstructible>(alloc, i); | ||
| static_assert(is_same_v<decltype(obj1), AllocatorArgConstructible>); | ||
| assert(obj1.x == i); | ||
|
|
||
| auto obj2 = make_obj_using_allocator<AllocatorConstructible>(alloc, i); | ||
| static_assert(is_same_v<decltype(obj2), AllocatorConstructible>); | ||
| assert(obj2.x == i); | ||
| } | ||
|
|
||
| { | ||
| allocator<AllocatorArgConstructible> alloc2; | ||
| auto ptr2 = alloc2.allocate(1); | ||
|
|
||
| uninitialized_construct_using_allocator(ptr2, alloc, i); | ||
| assert(ptr2->x == i); | ||
| destroy_at(ptr2); | ||
|
|
||
| alloc2.deallocate(ptr2, 1); | ||
|
|
||
| allocator<AllocatorConstructible> alloc3; | ||
| auto ptr3 = alloc3.allocate(1); | ||
|
|
||
| uninitialized_construct_using_allocator(ptr3, alloc, i); | ||
| assert(ptr3->x == i); | ||
| destroy_at(ptr3); | ||
|
|
||
| alloc3.deallocate(ptr3, 1); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| int main() { | ||
| test_P0475R1(); | ||
|
|
||
| assert(test_P0591R4()); | ||
| static_assert(test_P0591R4()); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Technically, these overloads of
uses_allocator_construction_argsare unimplementable since the Working Draft doesn't specify what types they return. (No change requested, just sharing my rage at the wording while I devise a curtly-worded issue report for LWG.)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.
Clearly they can return
anything :)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.
"Implementing the unimplementable" is a nice slogan though 😹
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.
Ha! We should talk to @MahmoudGSaleh about getting some morale budget to have T-shirts made for all of our contributors.