-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[libc++] Add the __is_replaceable type trait #132408
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 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b83e91a
[libc++] Add the __is_replaceable type trait
ldionne be5f620
Review comments
ldionne ef0afab
Undo stray formatting
ldionne 4bca641
Undo more formatting
ldionne 4c831ac
Adjust is_replaceable to disregard self-move
ldionne 174cc5a
Handle MSVC ABI
ldionne 7f7ff7a
Merge branch 'main' into review/tr-1-add-is_replaceable
ldionne 943972b
Be more strict about allocator replaceability
ldionne 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
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,62 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef _LIBCPP___TYPE_TRAITS_IS_REPLACEABLE_H | ||
| #define _LIBCPP___TYPE_TRAITS_IS_REPLACEABLE_H | ||
|
|
||
| #include <__config> | ||
| #include <__type_traits/enable_if.h> | ||
| #include <__type_traits/integral_constant.h> | ||
| #include <__type_traits/is_same.h> | ||
| #include <__type_traits/is_trivially_copyable.h> | ||
|
|
||
| #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | ||
| # pragma GCC system_header | ||
| #endif | ||
|
|
||
| _LIBCPP_BEGIN_NAMESPACE_STD | ||
|
|
||
| // A type is replaceable if, with `x` and `y` being different objects, `x = std::move(y)` is equivalent to: | ||
| // | ||
| // std::destroy_at(&x) | ||
| // std::construct_at(&x, std::move(y)) | ||
| // | ||
| // This allows turning a move-assignment into a sequence of destroy + move-construct, which | ||
| // is often more efficient. This is especially relevant when the move-construct is in fact | ||
| // part of a trivial relocation from somewhere else, in which case there is a huge win. | ||
| // | ||
| // Note that this requires language support in order to be really effective, but we | ||
| // currently emulate the base template with something very conservative. | ||
| template <class _Tp, class = void> | ||
| struct __is_replaceable : is_trivially_copyable<_Tp> {}; | ||
|
|
||
| template <class _Tp> | ||
| struct __is_replaceable<_Tp, __enable_if_t<is_same<_Tp, typename _Tp::__replaceable>::value> > : true_type {}; | ||
ldionne marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| template <class _Tp> | ||
| inline const bool __is_replaceable_v = __is_replaceable<_Tp>::value; | ||
|
|
||
| // Determines whether an allocator member of a container is replaceable. | ||
| // | ||
| // We take into account whether the allocator is propagated on assignments. If the allocator | ||
| // always compares equal, then it doesn't matter whether we propagate it or not on assignments, | ||
| // the result will be the same and we can just as much move-construct it instead. | ||
| // | ||
| // If the allocator does not always compare equal, we check whether it propagates on assignment | ||
| // and it is replaceable. | ||
| template <class _AllocatorTraits> | ||
| struct __container_allocator_is_replaceable | ||
| : integral_constant<bool, | ||
| _AllocatorTraits::is_always_equal::value || | ||
| (_AllocatorTraits::propagate_on_container_move_assignment::value && | ||
| _AllocatorTraits::propagate_on_container_copy_assignment::value && | ||
| __is_replaceable_v<typename _AllocatorTraits::allocator_type>)> {}; | ||
ldionne marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| _LIBCPP_END_NAMESPACE_STD | ||
|
|
||
| #endif // _LIBCPP___TYPE_TRAITS_IS_REPLACEABLE_H | ||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.