Skip to content

Commit

Permalink
rebase and address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
huixie90 committed Sep 28, 2024
1 parent f3c685c commit c99bc9f
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 138 deletions.
14 changes: 11 additions & 3 deletions libcxx/include/__type_traits/container_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,29 @@
// 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>
// __container_traits is a general purpose struct contains traits of containers' different operations.
// It currently only has one trait: `__emplacement_has_strong_exception_safety_guarantee`, but it's
// intended to be extended in the future.
// If a container does not support an operation. For example, `std::array` does not support `insert`
// or `emplace`, the trait of that operation will return false.
template <class _Container>
struct __container_traits {
using __emplacement_has_strong_exception_safety_guarantee = false_type;
// A trait that tells whether a single element insertion/emplacement via member function
// `insert(...)` or `emplace(...)` has strong exception guarantee, that is, if the function
// exits via an exception, the original container is unaffected
static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee = false;
};

_LIBCPP_END_NAMESPACE_STD
Expand Down
5 changes: 2 additions & 3 deletions libcxx/include/deque
Original file line number Diff line number Diff line change
Expand Up @@ -2618,9 +2618,8 @@ struct __container_traits<deque<_Tp, _Allocator> > {
// 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> >;
static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee =
_Or<is_nothrow_move_constructible<_Tp>, __is_cpp17_copy_insertable<_Allocator> >::value;
};

_LIBCPP_END_NAMESPACE_STD
Expand Down
6 changes: 3 additions & 3 deletions libcxx/include/forward_list
Original file line number Diff line number Diff line change
Expand Up @@ -1548,12 +1548,12 @@ erase(forward_list<_Tp, _Allocator>& __c, const _Up& __v) {
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],
// 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
// - 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;
static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee = true;
};

_LIBCPP_END_NAMESPACE_STD
Expand Down
6 changes: 3 additions & 3 deletions libcxx/include/list
Original file line number Diff line number Diff line change
Expand Up @@ -1719,12 +1719,12 @@ inline constexpr bool __format::__enable_insertable<std::list<wchar_t>> = true;
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],
// 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
// - 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;
static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee = true;
};

_LIBCPP_END_NAMESPACE_STD
Expand Down
4 changes: 2 additions & 2 deletions libcxx/include/map
Original file line number Diff line number Diff line change
Expand Up @@ -1650,7 +1650,7 @@ 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;
static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee = true;
};

template <class _Key, class _Tp, class _Compare = less<_Key>, class _Allocator = allocator<pair<const _Key, _Tp> > >
Expand Down Expand Up @@ -2172,7 +2172,7 @@ 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;
static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee = true;
};

_LIBCPP_END_NAMESPACE_STD
Expand Down
4 changes: 2 additions & 2 deletions libcxx/include/set
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,7 @@ 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;
static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee = true;
};

template <class _Key, class _Compare = less<_Key>, class _Allocator = allocator<_Key> >
Expand Down Expand Up @@ -1495,7 +1495,7 @@ 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;
static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee = true;
};

_LIBCPP_END_NAMESPACE_STD
Expand Down
7 changes: 5 additions & 2 deletions libcxx/include/unordered_map
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ template <class Key, class T, class Hash, class Pred, class Alloc>
#include <__ranges/from_range.h>
#include <__type_traits/container_traits.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/invoke.h>
#include <__type_traits/is_allocator.h>
#include <__type_traits/is_integral.h>
#include <__type_traits/remove_const.h>
Expand Down Expand Up @@ -1837,7 +1838,8 @@ struct __container_traits<unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc> > {
// 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&>;
static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee =
__nothrow_invokable<_Hash, const _Key&>::value;
};

template <class _Key,
Expand Down Expand Up @@ -2536,7 +2538,8 @@ struct __container_traits<unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc> >
// 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&>;
static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee =
__nothrow_invokable<_Hash, const _Key&>::value;
};

_LIBCPP_END_NAMESPACE_STD
Expand Down
7 changes: 5 additions & 2 deletions libcxx/include/unordered_set
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ template <class Value, class Hash, class Pred, class Alloc>
#include <__ranges/from_range.h>
#include <__type_traits/container_traits.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/invoke.h>
#include <__type_traits/is_allocator.h>
#include <__type_traits/is_integral.h>
#include <__type_traits/is_nothrow_assignable.h>
Expand Down Expand Up @@ -1190,7 +1191,8 @@ struct __container_traits<unordered_set<_Value, _Hash, _Pred, _Alloc> > {
// 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&>;
static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee =
__nothrow_invokable<_Hash, const _Value&>::value;
};

template <class _Value, class _Hash = hash<_Value>, class _Pred = equal_to<_Value>, class _Alloc = allocator<_Value> >
Expand Down Expand Up @@ -1809,7 +1811,8 @@ struct __container_traits<unordered_multiset<_Value, _Hash, _Pred, _Alloc> > {
// 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&>;
static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee =
__nothrow_invokable<_Hash, const _Value&>::value;
};

_LIBCPP_END_NAMESPACE_STD
Expand Down
5 changes: 2 additions & 3 deletions libcxx/include/vector
Original file line number Diff line number Diff line change
Expand Up @@ -3015,9 +3015,8 @@ struct __container_traits<vector<_Tp, _Allocator> > {
// 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> >;
static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee =
_Or<is_nothrow_move_constructible<_Tp>, __is_cpp17_copy_insertable<_Allocator> >::value;
};

_LIBCPP_END_NAMESPACE_STD
Expand Down
Loading

0 comments on commit c99bc9f

Please sign in to comment.