-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[libc++] constexpr deque #129368
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
base: main
Are you sure you want to change the base?
[libc++] constexpr deque #129368
Conversation
@llvm/pr-subscribers-libcxx Author: Nhat Nguyen (changkhothuychung) ChangesFull diff: https://github.com/llvm/llvm-project/pull/129368.diff 1 Files Affected:
diff --git a/libcxx/include/deque b/libcxx/include/deque
index 95200b4801d7f..c92b89aa9f1c0 100644
--- a/libcxx/include/deque
+++ b/libcxx/include/deque
@@ -2543,7 +2543,8 @@ inline void deque<_Tp, _Allocator>::clear() _NOEXCEPT {
}
template <class _Tp, class _Allocator>
-inline _LIBCPP_HIDE_FROM_ABI bool operator==(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
+inline _LIBCPP_HIDE_FROM_ABI constexpr bool
+operator==(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
const typename deque<_Tp, _Allocator>::size_type __sz = __x.size();
return __sz == __y.size() && std::equal(__x.begin(), __x.end(), __y.begin());
}
@@ -2578,7 +2579,7 @@ inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const deque<_Tp, _Allocator>& __x,
# else // _LIBCPP_STD_VER <= 17
template <class _Tp, class _Allocator>
-_LIBCPP_HIDE_FROM_ABI __synth_three_way_result<_Tp>
+_LIBCPP_HIDE_FROM_ABI __synth_three_way_result<_Tp> constexpr
operator<=>(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
return std::lexicographical_compare_three_way(__x.begin(), __x.end(), __y.begin(), __y.end(), std::__synth_three_way);
}
@@ -2586,14 +2587,14 @@ operator<=>(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y
# endif // _LIBCPP_STD_VER <= 17
template <class _Tp, class _Allocator>
-inline _LIBCPP_HIDE_FROM_ABI void swap(deque<_Tp, _Allocator>& __x, deque<_Tp, _Allocator>& __y)
+inline _LIBCPP_HIDE_FROM_ABI constexpr void swap(deque<_Tp, _Allocator>& __x, deque<_Tp, _Allocator>& __y)
_NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) {
__x.swap(__y);
}
# if _LIBCPP_STD_VER >= 20
template <class _Tp, class _Allocator, class _Up>
-inline _LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::size_type
+inline _LIBCPP_HIDE_FROM_ABI constexpr typename deque<_Tp, _Allocator>::size_type
erase(deque<_Tp, _Allocator>& __c, const _Up& __v) {
auto __old_size = __c.size();
__c.erase(std::remove(__c.begin(), __c.end(), __v), __c.end());
@@ -2601,7 +2602,7 @@ erase(deque<_Tp, _Allocator>& __c, const _Up& __v) {
}
template <class _Tp, class _Allocator, class _Predicate>
-inline _LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::size_type
+inline _LIBCPP_HIDE_FROM_ABI constexpr typename deque<_Tp, _Allocator>::size_type
erase_if(deque<_Tp, _Allocator>& __c, _Predicate __pred) {
auto __old_size = __c.size();
__c.erase(std::remove_if(__c.begin(), __c.end(), __pred), __c.end());
|
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.
Thanks! This PR should have Fixes #128656.
, and There are some changes to be done.
- Add
_LIBCPP_CONSTEXPR_SINCE_CXX26
to functions in<deque>
. - Modify test files (
.pass.cpp
) in thelibcxx/test/std/containers/sequences/deque
subdirectory. The following pattern can be used.
TEST_CONSTEXPR_CXX26 bool test() {
/* existing contents */
return true;
}
int main(int, char**) {
test();
#if TEST_STD_VER >= 26
static_assert(test());
#endif
}
- Add test macro entry for
__cpp_lib_constexpr_deque
(of value202502
, also in<deque>
) ingenerate_feature_test_macro_components.py
and then run the script.
Hmm it seems definitions of Am I missing something? |
Ah, cv |
✅ With the latest revision this PR passed the C/C++ code formatter. |
@changkhothuychung Can you please make sure the tests (at least relevant to your patch) pass locally before pushing changes? |
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.
having some questions
@@ -619,19 +620,20 @@ public: | |||
__alloc_traits::deallocate(__alloc(), *__i, __block_size); | |||
} | |||
|
|||
_LIBCPP_HIDE_FROM_ABI explicit deque(const allocator_type& __a) | |||
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 explicit deque(const allocator_type& __a) | |||
: __map_(__pointer_allocator(__a)), __start_(0), __size_(0), __alloc_(__a) { | |||
__annotate_new(0); |
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.
@frederick-vs-ja when trying to call the test in a constexpr case, via static_assert, I got error that __annotate_new (0)
cannot be used in a constant expression, this function is called within a constexpr constructor. How should I handle this case? Can __annotate_new
be made constexpr ?
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.
Yes. I think we can mark most underlying ASAN-related functions (which are not previously constexpr
) _LIBCPP_CONSTEXPR_SINCE_CXX26
and add if (__libcpp_is_constant_evaluated()) return;
to suitable places.
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.
@frederick-vs-ja can you give me an example of where I can put if (__libcpp_is_constant_evaluated()) return;
? I am not sure how I can apply that function.
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.
The error messages look a bit chaotic. Could you try again after finish adding _LIBCPP_CONSTEXPR_SINCE_CXX26
to <deque>
?
Im running into another case, an example of such case is located in the file When doing assertion In this case, we are trying to initialize The constructor @frederick-vs-ja Do you know how to fix this?
|
It seems that libc++'s |
I opened #136067 for the type issue with |
Fixes #128656