[libc++] Implement P1425R4 (Iterator pair constructors for std::stack and std::queue)
Implement P1425R4
Reviewed By: Quuxplusone, #libc, Mordante
Spies: Mordante, jloser, libcxx-commits, arichardson
Differential Revision: https://reviews.llvm.org/D115977
NOKEYCHECK=True
GitOrigin-RevId: f3aed3698185dd4a03b210a9d73be4dc1efc580f
diff --git a/include/queue b/include/queue
index 9fad802..9e1257b 100644
--- a/include/queue
+++ b/include/queue
@@ -41,6 +41,8 @@
explicit queue(const container_type& c);
explicit queue(container_type&& c)
+ template<class InputIterator>
+ queue(InputIterator first, InputIterator last); // since C++23
template <class Alloc>
explicit queue(const Alloc& a);
template <class Alloc>
@@ -51,6 +53,8 @@
queue(const queue& q, const Alloc& a);
template <class Alloc>
queue(queue&& q, const Alloc& a);
+ template <class InputIterator, class Alloc>
+ queue(InputIterator first, InputIterator last, const Alloc&); // since C++23
bool empty() const;
size_type size() const;
@@ -71,9 +75,17 @@
template<class Container>
queue(Container) -> queue<typename Container::value_type, Container>; // C++17
+template<class InputIterator>
+ queue(InputIterator, InputIterator) -> queue<iter-value-type<InputIterator>>; // since C++23
+
template<class Container, class Allocator>
queue(Container, Allocator) -> queue<typename Container::value_type, Container>; // C++17
+template<class InputIterator, class Allocator>
+ queue(InputIterator, InputIterator, Allocator)
+ -> queue<iter-value-type<InputIterator>,
+ deque<iter-value-type<InputIterator>, Allocator>>; // since C++23
+
template <class T, class Container>
bool operator==(const queue<T, Container>& x,const queue<T, Container>& y);
@@ -206,12 +218,14 @@
*/
#include <__config>
+#include <__iterator/iterator_traits.h>
#include <__memory/uses_allocator.h>
#include <__utility/forward.h>
#include <algorithm>
#include <compare>
#include <deque>
#include <functional>
+#include <type_traits>
#include <vector>
#include <version>
@@ -256,6 +270,20 @@
_LIBCPP_INLINE_VISIBILITY
queue(const queue& __q) : c(__q.c) {}
+#if _LIBCPP_STD_VER > 20
+ template <class _InputIterator,
+ class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>>
+ _LIBCPP_HIDE_FROM_ABI
+ queue(_InputIterator __first, _InputIterator __last) : c(__first, __last) {}
+
+ template <class _InputIterator,
+ class _Alloc,
+ class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
+ class = __enable_if_t<uses_allocator<container_type, _Alloc>::value>>
+ _LIBCPP_HIDE_FROM_ABI
+ queue(_InputIterator __first, _InputIterator __second, const _Alloc& __alloc) : c(__first, __second, __alloc) {}
+#endif
+
_LIBCPP_INLINE_VISIBILITY
queue& operator=(const queue& __q) {c = __q.c; return *this;}
@@ -359,7 +387,7 @@
operator< (const queue<_T1, _C1>& __x,const queue<_T1, _C1>& __y);
};
-#if _LIBCPP_STD_VER >= 17
+#if _LIBCPP_STD_VER > 14
template<class _Container,
class = enable_if_t<!__is_allocator<_Container>::value>
>
@@ -375,6 +403,20 @@
-> queue<typename _Container::value_type, _Container>;
#endif
+#if _LIBCPP_STD_VER > 20
+template <class _InputIterator,
+ class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>>
+queue(_InputIterator, _InputIterator)
+ -> queue<__iter_value_type<_InputIterator>>;
+
+template <class _InputIterator,
+ class _Alloc,
+ class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
+ class = __enable_if_t<__is_allocator<_Alloc>::value>>
+queue(_InputIterator, _InputIterator, _Alloc)
+ -> queue<__iter_value_type<_InputIterator>, deque<__iter_value_type<_InputIterator>, _Alloc>>;
+#endif
+
template <class _Tp, class _Container>
inline _LIBCPP_INLINE_VISIBILITY
bool
diff --git a/include/stack b/include/stack
index 3cf6cd2..ad65d7b 100644
--- a/include/stack
+++ b/include/stack
@@ -41,11 +41,14 @@
explicit stack(const container_type& c);
explicit stack(container_type&& c);
+ template <class InputIterator> stack(InputIterator first, InputIterator last); // since C++23
template <class Alloc> explicit stack(const Alloc& a);
template <class Alloc> stack(const container_type& c, const Alloc& a);
template <class Alloc> stack(container_type&& c, const Alloc& a);
template <class Alloc> stack(const stack& c, const Alloc& a);
template <class Alloc> stack(stack&& c, const Alloc& a);
+ template<class InputIterator, class Alloc>
+ stack(InputIterator first, InputIterator last, const Alloc&); // since C++23
bool empty() const;
size_type size() const;
@@ -63,9 +66,17 @@
template<class Container>
stack(Container) -> stack<typename Container::value_type, Container>; // C++17
+template<class InputIterator>
+ stack(InputIterator, InputIterator) -> stack<iter-value-type<InputIterator>>; // since C++23
+
template<class Container, class Allocator>
stack(Container, Allocator) -> stack<typename Container::value_type, Container>; // C++17
+template<class InputIterator, class Allocator>
+ stack(InputIterator, InputIterator, Allocator)
+ -> stack<iter-value-type<InputIterator>,
+ deque<iter-value-type<InputIterator>, Allocator>>; // since C++23
+
template <class T, class Container>
bool operator==(const stack<T, Container>& x, const stack<T, Container>& y);
template <class T, class Container>
@@ -88,9 +99,11 @@
*/
#include <__config>
+#include <__iterator/iterator_traits.h>
#include <__memory/uses_allocator.h>
#include <__utility/forward.h>
#include <deque>
+#include <type_traits>
#include <version>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -184,6 +197,20 @@
: c(_VSTD::move(__s.c), __a) {}
#endif // _LIBCPP_CXX03_LANG
+#if _LIBCPP_STD_VER > 20
+ template <class _InputIterator,
+ class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>>
+ _LIBCPP_HIDE_FROM_ABI
+ stack(_InputIterator __first, _InputIterator __last) : c(__first, __last) {}
+
+ template <class _InputIterator,
+ class _Alloc,
+ class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
+ class = __enable_if_t<uses_allocator<container_type, _Alloc>::value>>
+ _LIBCPP_HIDE_FROM_ABI
+ stack(_InputIterator __first, _InputIterator __last, const _Alloc& __alloc) : c(__first, __last, __alloc) {}
+#endif
+
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
bool empty() const {return c.empty();}
_LIBCPP_INLINE_VISIBILITY
@@ -232,7 +259,7 @@
operator< (const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
};
-#if _LIBCPP_STD_VER >= 17
+#if _LIBCPP_STD_VER > 14
template<class _Container,
class = enable_if_t<!__is_allocator<_Container>::value>
>
@@ -248,6 +275,20 @@
-> stack<typename _Container::value_type, _Container>;
#endif
+#if _LIBCPP_STD_VER > 20
+template<class _InputIterator,
+ class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>>
+stack(_InputIterator, _InputIterator)
+ -> stack<__iter_value_type<_InputIterator>>;
+
+template<class _InputIterator,
+ class _Alloc,
+ class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
+ class = __enable_if_t<__is_allocator<_Alloc>::value>>
+stack(_InputIterator, _InputIterator, _Alloc)
+ -> stack<__iter_value_type<_InputIterator>, deque<__iter_value_type<_InputIterator>, _Alloc>>;
+#endif
+
template <class _Tp, class _Container>
inline _LIBCPP_INLINE_VISIBILITY
bool
diff --git a/include/version b/include/version
index db67b2e..6774e31 100644
--- a/include/version
+++ b/include/version
@@ -14,6 +14,7 @@
version synopsis
Macro name Value Headers
+__cpp_lib_adaptor_iterator_pair_constructor 202106L <queue> <stack>
__cpp_lib_addressof_constexpr 201603L <memory>
__cpp_lib_allocate_at_least 202106L <memory>
__cpp_lib_allocator_traits_is_always_equal 201411L <deque> <forward_list> <list>
@@ -358,6 +359,7 @@
#endif
#if _LIBCPP_STD_VER > 20
+# define __cpp_lib_adaptor_iterator_pair_constructor 202106L
// # define __cpp_lib_allocate_at_least 202106L
// # define __cpp_lib_associative_heterogeneous_erasure 202110L
# define __cpp_lib_byteswap 202110L