[libcxx-commits] [libcxx] [libc++] Add containter_traits (prework for `std::flat_map`) (PR #109578)
via libcxx-commits
libcxx-commits at lists.llvm.org
Sun Sep 22 08:08:02 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Hui (huixie90)
<details>
<summary>Changes</summary>
This PR is extracted from https://github.com/llvm/llvm-project/pull/98643, as per code review request
https://github.com/llvm/llvm-project/pull/98643#discussion_r1768967793
---
Patch is 25.15 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/109578.diff
12 Files Affected:
- (modified) libcxx/include/CMakeLists.txt (+1)
- (added) libcxx/include/__type_traits/container_traits.h (+28)
- (modified) libcxx/include/deque (+14)
- (modified) libcxx/include/forward_list (+12)
- (modified) libcxx/include/list (+12)
- (modified) libcxx/include/map (+17)
- (modified) libcxx/include/module.modulemap (+1)
- (modified) libcxx/include/set (+17)
- (modified) libcxx/include/unordered_map (+19)
- (modified) libcxx/include/unordered_set (+19)
- (modified) libcxx/include/vector (+15)
- (added) libcxx/test/libcxx/containers/container_traits.compile.pass.cpp (+173)
``````````diff
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 8c61009167ddce..350d62009a247e 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -748,6 +748,7 @@ set(files
__type_traits/common_type.h
__type_traits/conditional.h
__type_traits/conjunction.h
+ __type_traits/container_traits.h
__type_traits/copy_cv.h
__type_traits/copy_cvref.h
__type_traits/datasizeof.h
diff --git a/libcxx/include/__type_traits/container_traits.h b/libcxx/include/__type_traits/container_traits.h
new file mode 100644
index 00000000000000..a00433da249a67
--- /dev/null
+++ b/libcxx/include/__type_traits/container_traits.h
@@ -0,0 +1,28 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// 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_CONTAINER_TRAITS_H
+#define _LIBCPP___TYPE_TRAITS_CONTAINER_TRAITS_H
+
+#include <__config>
+#include <__type_traits/integral_constant.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <class _Tp>
+struct __container_traits {
+ using __emplacement_has_strong_exception_safety_guarantee = false_type;
+};
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___TYPE_TRAITS_CONTAINER_TRAITS_H
diff --git a/libcxx/include/deque b/libcxx/include/deque
index bab0526629f0f8..a13477a35cfe5a 100644
--- a/libcxx/include/deque
+++ b/libcxx/include/deque
@@ -220,6 +220,8 @@ template <class T, class Allocator, class Predicate>
#include <__ranges/size.h>
#include <__split_buffer>
#include <__type_traits/conditional.h>
+#include <__type_traits/container_traits.h>
+#include <__type_traits/disjunction.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/is_allocator.h>
#include <__type_traits/is_convertible.h>
@@ -2609,6 +2611,18 @@ inline constexpr bool __format::__enable_insertable<std::deque<wchar_t>> = true;
#endif // _LIBCPP_STD_VER >= 20
+template <class _Tp, class _Allocator>
+struct __container_traits<deque<_Tp, _Allocator> > {
+ // http://eel.is/c++draft/deque.modifiers#3
+ // If an exception is thrown other than by the copy constructor, move constructor, assignment operator, or move
+ // assignment operator of T, there are no effects. If an exception is thrown while inserting a single element at
+ // either end, there are no effects. Otherwise, if an exception is thrown by the move constructor of a
+ // non-Cpp17CopyInsertable T, the effects are unspecified.
+ using __emplacement_has_strong_exception_safety_guarantee =
+ _Or<is_nothrow_move_constructible<_Tp>,
+ __is_cpp17_copy_insertable<typename deque<_Tp, _Allocator>::allocator_type> >;
+};
+
_LIBCPP_END_NAMESPACE_STD
#if _LIBCPP_STD_VER >= 17
diff --git a/libcxx/include/forward_list b/libcxx/include/forward_list
index c5ae8add511cf8..c581fc17fe5378 100644
--- a/libcxx/include/forward_list
+++ b/libcxx/include/forward_list
@@ -218,6 +218,7 @@ template <class T, class Allocator, class Predicate>
#include <__ranges/container_compatible_range.h>
#include <__ranges/from_range.h>
#include <__type_traits/conditional.h>
+#include <__type_traits/container_traits.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/is_allocator.h>
#include <__type_traits/is_const.h>
@@ -1544,6 +1545,17 @@ erase(forward_list<_Tp, _Allocator>& __c, const _Up& __v) {
}
#endif
+template <class _Tp, class _Allocator>
+struct __container_traits<forward_list<_Tp, _Allocator> > {
+ // http://eel.is/c++draft/container.reqmts
+ // 66 Unless otherwise specified (see [associative.reqmts.except], [unord.req.except], [deque.modifiers],
+ // [inplace.vector.modifiers], and [vector.modifiers]) all container types defined in this Clause meet the following
+ // additional requirements:
+ // - (66.1) If an exception is thrown by an insert() or emplace() function while inserting a single element, that
+ // function has no effects.
+ using __emplacement_has_strong_exception_safety_guarantee = true_type;
+};
+
_LIBCPP_END_NAMESPACE_STD
#if _LIBCPP_STD_VER >= 17
diff --git a/libcxx/include/list b/libcxx/include/list
index 05234f7696c6f4..235bac2f5a66b4 100644
--- a/libcxx/include/list
+++ b/libcxx/include/list
@@ -225,6 +225,7 @@ template <class T, class Allocator, class Predicate>
#include <__ranges/container_compatible_range.h>
#include <__ranges/from_range.h>
#include <__type_traits/conditional.h>
+#include <__type_traits/container_traits.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/is_allocator.h>
#include <__type_traits/is_nothrow_assignable.h>
@@ -1715,6 +1716,17 @@ inline constexpr bool __format::__enable_insertable<std::list<wchar_t>> = true;
#endif // _LIBCPP_STD_VER >= 20
+template <class _Tp, class _Allocator>
+struct __container_traits<list<_Tp, _Allocator> > {
+ // http://eel.is/c++draft/container.reqmts
+ // 66 Unless otherwise specified (see [associative.reqmts.except], [unord.req.except], [deque.modifiers],
+ // [inplace.vector.modifiers], and [vector.modifiers]) all container types defined in this Clause meet the following
+ // additional requirements:
+ // - (66.1) If an exception is thrown by an insert() or emplace() function while inserting a single element, that
+ // function has no effects.
+ using __emplacement_has_strong_exception_safety_guarantee = true_type;
+};
+
_LIBCPP_END_NAMESPACE_STD
#if _LIBCPP_STD_VER >= 17
diff --git a/libcxx/include/map b/libcxx/include/map
index 7fca4c8a0872fd..7ed4380beb53dd 100644
--- a/libcxx/include/map
+++ b/libcxx/include/map
@@ -594,6 +594,7 @@ erase_if(multimap<Key, T, Compare, Allocator>& c, Predicate pred); // C++20
#include <__ranges/container_compatible_range.h>
#include <__ranges/from_range.h>
#include <__tree>
+#include <__type_traits/container_traits.h>
#include <__type_traits/is_allocator.h>
#include <__type_traits/remove_const.h>
#include <__type_traits/type_identity.h>
@@ -1644,6 +1645,14 @@ erase_if(map<_Key, _Tp, _Compare, _Allocator>& __c, _Predicate __pred) {
}
#endif
+template <class _Key, class _Tp, class _Compare, class _Allocator>
+struct __container_traits<map<_Key, _Tp, _Compare, _Allocator> > {
+ // http://eel.is/c++draft/associative.reqmts.except#2
+ // For associative containers, if an exception is thrown by any operation from within
+ // an insert or emplace function inserting a single element, the insertion has no effect.
+ using __emplacement_has_strong_exception_safety_guarantee = true_type;
+};
+
template <class _Key, class _Tp, class _Compare = less<_Key>, class _Allocator = allocator<pair<const _Key, _Tp> > >
class _LIBCPP_TEMPLATE_VIS multimap {
public:
@@ -2158,6 +2167,14 @@ erase_if(multimap<_Key, _Tp, _Compare, _Allocator>& __c, _Predicate __pred) {
}
#endif
+template <class _Key, class _Tp, class _Compare, class _Allocator>
+struct __container_traits<multimap<_Key, _Tp, _Compare, _Allocator> > {
+ // http://eel.is/c++draft/associative.reqmts.except#2
+ // For associative containers, if an exception is thrown by any operation from within
+ // an insert or emplace function inserting a single element, the insertion has no effect.
+ using __emplacement_has_strong_exception_safety_guarantee = true_type;
+};
+
_LIBCPP_END_NAMESPACE_STD
#if _LIBCPP_STD_VER >= 17
diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap
index ef4a242cf8bf7f..2f703ecba3b76f 100644
--- a/libcxx/include/module.modulemap
+++ b/libcxx/include/module.modulemap
@@ -1864,6 +1864,7 @@ module std_private_type_traits_common_type [system
}
module std_private_type_traits_conditional [system] { header "__type_traits/conditional.h" }
module std_private_type_traits_conjunction [system] { header "__type_traits/conjunction.h" }
+module std_private_type_traits_container_traits [system] { header "__type_traits/container_traits.h" }
module std_private_type_traits_copy_cv [system] { header "__type_traits/copy_cv.h" }
module std_private_type_traits_copy_cvref [system] { header "__type_traits/copy_cvref.h" }
module std_private_type_traits_datasizeof [system] { header "__type_traits/datasizeof.h" }
diff --git a/libcxx/include/set b/libcxx/include/set
index 0c2ca64139e0d3..8cbfca7e3b1495 100644
--- a/libcxx/include/set
+++ b/libcxx/include/set
@@ -531,6 +531,7 @@ erase_if(multiset<Key, Compare, Allocator>& c, Predicate pred); // C++20
#include <__ranges/container_compatible_range.h>
#include <__ranges/from_range.h>
#include <__tree>
+#include <__type_traits/container_traits.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/is_allocator.h>
#include <__type_traits/is_nothrow_assignable.h>
@@ -1022,6 +1023,14 @@ erase_if(set<_Key, _Compare, _Allocator>& __c, _Predicate __pred) {
}
#endif
+template <class _Key, class _Compare, class _Allocator>
+struct __container_traits<set<_Key, _Compare, _Allocator> > {
+ // http://eel.is/c++draft/associative.reqmts.except#2
+ // For associative containers, if an exception is thrown by any operation from within
+ // an insert or emplace function inserting a single element, the insertion has no effect.
+ using __emplacement_has_strong_exception_safety_guarantee = true_type;
+};
+
template <class _Key, class _Compare = less<_Key>, class _Allocator = allocator<_Key> >
class _LIBCPP_TEMPLATE_VIS multiset {
public:
@@ -1481,6 +1490,14 @@ erase_if(multiset<_Key, _Compare, _Allocator>& __c, _Predicate __pred) {
}
#endif
+template <class _Key, class _Compare, class _Allocator>
+struct __container_traits<multiset<_Key, _Compare, _Allocator> > {
+ // http://eel.is/c++draft/associative.reqmts.except#2
+ // For associative containers, if an exception is thrown by any operation from within
+ // an insert or emplace function inserting a single element, the insertion has no effect.
+ using __emplacement_has_strong_exception_safety_guarantee = true_type;
+};
+
_LIBCPP_END_NAMESPACE_STD
#if _LIBCPP_STD_VER >= 17
diff --git a/libcxx/include/unordered_map b/libcxx/include/unordered_map
index 0d71a51ee9e729..8e04e80c00334a 100644
--- a/libcxx/include/unordered_map
+++ b/libcxx/include/unordered_map
@@ -604,6 +604,7 @@ template <class Key, class T, class Hash, class Pred, class Alloc>
#include <__ranges/concepts.h>
#include <__ranges/container_compatible_range.h>
#include <__ranges/from_range.h>
+#include <__type_traits/container_traits.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/is_allocator.h>
#include <__type_traits/is_integral.h>
@@ -1830,6 +1831,15 @@ inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const unordered_map<_Key, _Tp, _Has
#endif
+template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
+struct __container_traits<unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc> > {
+ // http://eel.is/c++draft/unord.req.except#2
+ // For unordered associative containers, if an exception is thrown by any operation
+ // other than the container's hash function from within an insert or emplace function
+ // inserting a single element, the insertion has no effect.
+ using __emplacement_has_strong_exception_safety_guarantee = __nothrow_invokable<_Hash, const _Key&>;
+};
+
template <class _Key,
class _Tp,
class _Hash = hash<_Key>,
@@ -2520,6 +2530,15 @@ inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const unordered_multimap<_Key, _Tp,
#endif
+template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
+struct __container_traits<unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc> > {
+ // http://eel.is/c++draft/unord.req.except#2
+ // For unordered associative containers, if an exception is thrown by any operation
+ // other than the container's hash function from within an insert or emplace function
+ // inserting a single element, the insertion has no effect.
+ using __emplacement_has_strong_exception_safety_guarantee = __nothrow_invokable<_Hash, const _Key&>;
+};
+
_LIBCPP_END_NAMESPACE_STD
#if _LIBCPP_STD_VER >= 17
diff --git a/libcxx/include/unordered_set b/libcxx/include/unordered_set
index 2b09c72b866b03..a0a5be14b165a3 100644
--- a/libcxx/include/unordered_set
+++ b/libcxx/include/unordered_set
@@ -550,6 +550,7 @@ template <class Value, class Hash, class Pred, class Alloc>
#include <__ranges/concepts.h>
#include <__ranges/container_compatible_range.h>
#include <__ranges/from_range.h>
+#include <__type_traits/container_traits.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/is_allocator.h>
#include <__type_traits/is_integral.h>
@@ -1183,6 +1184,15 @@ inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const unordered_set<_Value, _Hash,
#endif
+template <class _Value, class _Hash, class _Pred, class _Alloc>
+struct __container_traits<unordered_set<_Value, _Hash, _Pred, _Alloc> > {
+ // http://eel.is/c++draft/unord.req.except#2
+ // For unordered associative containers, if an exception is thrown by any operation
+ // other than the container's hash function from within an insert or emplace function
+ // inserting a single element, the insertion has no effect.
+ using __emplacement_has_strong_exception_safety_guarantee = __nothrow_invokable<_Hash, const _Value&>;
+};
+
template <class _Value, class _Hash = hash<_Value>, class _Pred = equal_to<_Value>, class _Alloc = allocator<_Value> >
class _LIBCPP_TEMPLATE_VIS unordered_multiset {
public:
@@ -1793,6 +1803,15 @@ inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const unordered_multiset<_Value, _H
#endif
+template <class _Value, class _Hash, class _Pred, class _Alloc>
+struct __container_traits<unordered_multiset<_Value, _Hash, _Pred, _Alloc> > {
+ // http://eel.is/c++draft/unord.req.except#2
+ // For unordered associative containers, if an exception is thrown by any operation
+ // other than the container's hash function from within an insert or emplace function
+ // inserting a single element, the insertion has no effect.
+ using __emplacement_has_strong_exception_safety_guarantee = __nothrow_invokable<_Hash, const _Value&>;
+};
+
_LIBCPP_END_NAMESPACE_STD
#if _LIBCPP_STD_VER >= 17
diff --git a/libcxx/include/vector b/libcxx/include/vector
index 7d3aac5989a48c..219f54a670635a 100644
--- a/libcxx/include/vector
+++ b/libcxx/include/vector
@@ -356,6 +356,8 @@ template<class T, class charT> requires is-vector-bool-reference<T> // Since C++
#include <__ranges/size.h>
#include <__split_buffer>
#include <__type_traits/conditional.h>
+#include <__type_traits/container_traits.h>
+#include <__type_traits/disjunction.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/is_allocator.h>
#include <__type_traits/is_constructible.h>
@@ -3005,6 +3007,19 @@ public:
};
#endif // _LIBCPP_STD_VER >= 23
+template <class _Tp, class _Allocator>
+struct __container_traits<vector<_Tp, _Allocator> > {
+ // http://eel.is/c++draft/vector.modifiers#2
+ // If an exception is thrown other than by the copy constructor, move constructor, assignment operator, or move
+ // assignment operator of T or by any InputIterator operation, there are no effects. If an exception is thrown while
+ // inserting a single element at the end and T is Cpp17CopyInsertable or is_nothrow_move_constructible_v<T> is true,
+ // there are no effects. Otherwise, if an exception is thrown by the move constructor of a non-Cpp17CopyInsertable T,
+ // the effects are unspecified.
+ using __emplacement_has_strong_exception_safety_guarantee =
+ _Or<is_nothrow_move_constructible<_Tp>,
+ __is_cpp17_copy_insertable<typename vector<_Tp, _Allocator>::allocator_type> >;
+};
+
_LIBCPP_END_NAMESPACE_STD
#if _LIBCPP_STD_VER >= 17
diff --git a/libcxx/test/libcxx/containers/container_traits.compile.pass.cpp b/libcxx/test/libcxx/containers/container_traits.compile.pass.cpp
new file mode 100644
index 00000000000000..f6ea338c803fba
--- /dev/null
+++ b/libcxx/test/libcxx/containers/container_traits.compile.pass.cpp
@@ -0,0 +1,173 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// <__type_traits/container_traits.h>
+//
+
+#include <__type_traits/container_traits.h>
+
+#include <deque>
+#include <forward_list>
+#include <list>
+#include <vector>
+#include <map>
+#include <set>
+#include <unordered_map>
+#include <unordered_set>
+
+#include "test_allocator.h"
+#include "test_macros.h"
+#include "MoveOnly.h"
+
+struct ThrowOnMove {
+ ThrowOnMove();
+ ThrowOnMove(const ThrowOnMove&) _NOEXCEPT_(false);
+ ThrowOnMove(ThrowOnMove&&) _NOEXCEPT_(false);
+ ThrowOnMove& operator=(ThrowOnMove&&) _NOEXCEPT_(false);
+ ThrowOnMove& operator=(const ThrowOnMove&) _NOEXCEPT_(false);
+
+ bool operator<(ThrowOnMove const&) const;
+ bool operator==(ThrowOnMove const&) const;
+};
+
+struct NonCopyThrowOnMove {
+ NonCopyThrowOnMove();
+ NonCopyThrowOnMove(ThrowOnMove&&) _NOEXCEPT_(false);
+ NonCopyThrowOnMove(const NonCopyThrowOnMove&) = delete;
+ NonCopyThrowOnMove& operator=(ThrowOnMove&&) _NOEXCEPT_(false);
+ NonCopyThrowOnMove& operator=(const NonCopyThrowOnMove&) = delete;
+
+ bool operator<(NonCopyThrowOnMove const&) const;
+ bool operator==(NonCopyThrowOnMove const&) const;
+};
+
+struct ThrowingHash {
+ template <class T>
+ std::size_t operator()(const T&) const _NOEXCEPT_(false);
+};
+
+struct NoThrowHash {
+ template <class T>
+ std::size_t operator()(const T&) const _NOEXCEPT;
+};
+
+template <class T, bool Expected>
+void test_emplacement_strong_exception() {
+ static_assert(std::__container_traits<T>::__emplacement_has_strong_exception_safety_guarantee::value == Expected, "");
+}
+
+void test() {
+ test_emplacement_strong_exception<std::list<int>, true>();
+ test_emplacement_strong_exception<std::list<int, test_allocator<int> >, true>();
+ test_emplacement_strong_exception<std::list<MoveOnly>, true>();
+ test_emplacement_strong_exception<std::list<ThrowOnMove>, true>();
+ test_emplacement_strong_exception<std::list<NonCopyThrowOnMove>, true>();
+
+ test_emplacement_strong_exception<std::forward_list<int>, true>();
+ test_emplacement_strong_exception<std::forward_list<int, test_allocator<int> >, true>();
+ test_emplacement_strong_exception<std::forward_list<MoveOnly>, true>();
+ test_emplacement_strong_exception<std::forward_list<ThrowOnMove>, true>();
+ test_emplacement_strong_exception<std::forward_list<NonCopyThrowOnMove>, true>();
+
+ test_emplacement_strong_exception<std::deque<int>, true>();
+ test_emplacement_strong_exception<std::deque<int, test_allocator<int> >, true>();
+ test_emplacement_strong_exception<std::deque<MoveOnly>, true>();
+ test_emplacement_strong_exception<std::deque<ThrowOnMove>, true>();
+ test_emplacement_strong_exception<std::deque<NonCopyThrowOnMove>, false>();
+
+ test_emplacement_strong_exception<std::vector<int>, true>();
+ test_emplacement_strong_exception<std::vector<int, test_allocator<int> >, true>();
+ test_emplacement_strong_exception<std::vector<MoveOnly>, true>();
+ test_emplacement_strong_exception<std::vector<ThrowOnMove>, true>();
+ test_emplacement_strong_exception<std::vector<NonCopyThrowOnMove>, false>();
+
+ test_emplacement_strong_exception<std::set<int>, true>();
+ test_emplacement_strong_exception<std::set<int, std::less<int>, test_allocator<int> >, true>();
+ test_emplacement_strong_exception<std::set<MoveOnly>, true>();
+ test_emplacement_strong_exception<std::set<ThrowOnMove>, true>();
+ test_emplacement_strong_exception<std::set<NonCopyThrowOnMove>, true>();
+
+ test_emplacement_strong_exception<std::multiset<int>, true>();
+ test_emplacement_strong_exception<std::multiset<int, std::less<int>, test_allocator<int> >, true>();
+ test_emplacement_strong_exc...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/109578
More information about the libcxx-commits
mailing list