Deduction guides for the container adaptors - queue, stack, and priority_queue

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@332927 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Marshall Clow 2018-05-22 01:57:53 +00:00
parent 32bc2e298c
commit 0e5d70781a
6 changed files with 367 additions and 0 deletions

View File

@ -69,6 +69,12 @@ public:
void swap(queue& q) noexcept(is_nothrow_swappable_v<Container>) void swap(queue& q) noexcept(is_nothrow_swappable_v<Container>)
}; };
template<class Container>
queue(Container) -> queue<typename Container::value_type, Container>; // C++17
template<class Container, class Allocator>
queue(Container, Allocator) -> queue<typename Container::value_type, Container>; // C++17
template <class T, class Container> template <class T, class Container>
bool operator==(const queue<T, Container>& x,const queue<T, Container>& y); bool operator==(const queue<T, Container>& x,const queue<T, Container>& y);
@ -157,6 +163,20 @@ public:
is_nothrow_swappable_v<Comp>) is_nothrow_swappable_v<Comp>)
}; };
template <class Compare, class Container>
priority_queue(Compare, Container)
-> priority_queue<typename Container::value_type, Container, Compare>; // C++17
template<class InputIterator,
class Compare = less<typename iterator_traits<InputIterator>::value_type>,
class Container = vector<typename iterator_traits<InputIterator>::value_type>>
priority_queue(InputIterator, InputIterator, Compare = Compare(), Container = Container())
-> priority_queue<typename iterator_traits<InputIterator>::value_type, Container, Compare>; // C++17
template<class Compare, class Container, class Allocator>
priority_queue(Compare, Container, Allocator)
-> priority_queue<typename Container::value_type, Container, Compare>; // C++17
template <class T, class Container, class Compare> template <class T, class Container, class Compare>
void swap(priority_queue<T, Container, Compare>& x, void swap(priority_queue<T, Container, Compare>& x,
priority_queue<T, Container, Compare>& y) priority_queue<T, Container, Compare>& y)
@ -321,6 +341,22 @@ public:
operator< (const queue<_T1, _C1>& __x,const queue<_T1, _C1>& __y); operator< (const queue<_T1, _C1>& __x,const queue<_T1, _C1>& __y);
}; };
#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
template<class _Container,
class = typename enable_if<!__is_allocator<_Container>::value, nullptr_t>::type
>
queue(_Container)
-> queue<typename _Container::value_type, _Container>;
template<class _Container,
class _Alloc,
class = typename enable_if<!__is_allocator<_Container>::value, nullptr_t>::type,
class = typename enable_if< __is_allocator<_Alloc>::value, nullptr_t>::type
>
queue(_Container, _Alloc)
-> queue<typename _Container::value_type, _Container>;
#endif
template <class _Tp, class _Container> template <class _Tp, class _Container>
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY
bool bool
@ -515,6 +551,36 @@ public:
__is_nothrow_swappable<value_compare>::value); __is_nothrow_swappable<value_compare>::value);
}; };
#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
template <class _Compare,
class _Container,
class = typename enable_if<!__is_allocator<_Compare>::value, nullptr_t>::type,
class = typename enable_if<!__is_allocator<_Container>::value, nullptr_t>::type
>
priority_queue(_Compare, _Container)
-> priority_queue<typename _Container::value_type, _Container, _Compare>;
template<class _InputIterator,
class _Compare = less<typename iterator_traits<_InputIterator>::value_type>,
class _Container = vector<typename iterator_traits<_InputIterator>::value_type>,
class = typename enable_if< __is_input_iterator<_InputIterator>::value, nullptr_t>::type,
class = typename enable_if<!__is_allocator<_Compare>::value, nullptr_t>::type,
class = typename enable_if<!__is_allocator<_Container>::value, nullptr_t>::type
>
priority_queue(_InputIterator, _InputIterator, _Compare = _Compare(), _Container = _Container())
-> priority_queue<typename iterator_traits<_InputIterator>::value_type, _Container, _Compare>;
template<class _Compare,
class _Container,
class _Alloc,
class = typename enable_if<!__is_allocator<_Compare>::value, nullptr_t>::type,
class = typename enable_if<!__is_allocator<_Container>::value, nullptr_t>::type,
class = typename enable_if< __is_allocator<_Alloc>::value, nullptr_t>::type
>
priority_queue(_Compare, _Container, _Alloc)
-> priority_queue<typename _Container::value_type, _Container, _Compare>;
#endif
template <class _Tp, class _Container, class _Compare> template <class _Tp, class _Container, class _Compare>
inline inline
priority_queue<_Tp, _Container, _Compare>::priority_queue(const _Compare& __comp, priority_queue<_Tp, _Container, _Compare>::priority_queue(const _Compare& __comp,

View File

@ -61,6 +61,12 @@ public:
void swap(stack& c) noexcept(is_nothrow_swappable_v<Container>) void swap(stack& c) noexcept(is_nothrow_swappable_v<Container>)
}; };
template<class Container>
stack(Container) -> stack<typename Container::value_type, Container>; // C++17
template<class Container, class Allocator>
stack(Container, Allocator) -> stack<typename Container::value_type, Container>; // C++17
template <class T, class Container> template <class T, class Container>
bool operator==(const stack<T, Container>& x, const stack<T, Container>& y); bool operator==(const stack<T, Container>& x, const stack<T, Container>& y);
template <class T, class Container> template <class T, class Container>
@ -229,6 +235,22 @@ public:
operator< (const stack<T1, _C1>& __x, const stack<T1, _C1>& __y); operator< (const stack<T1, _C1>& __x, const stack<T1, _C1>& __y);
}; };
#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
template<class _Container,
class = typename enable_if<!__is_allocator<_Container>::value, nullptr_t>::type
>
stack(_Container)
-> stack<typename _Container::value_type, _Container>;
template<class _Container,
class _Alloc,
class = typename enable_if<!__is_allocator<_Container>::value, nullptr_t>::type,
class = typename enable_if< __is_allocator<_Alloc>::value, nullptr_t>::type
>
stack(_Container, _Alloc)
-> stack<typename _Container::value_type, _Container>;
#endif
template <class _Tp, class _Container> template <class _Tp, class _Container>
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY
bool bool

View File

@ -0,0 +1,46 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <queue>
// UNSUPPORTED: c++98, c++03, c++11, c++14
// UNSUPPORTED: libcpp-no-deduction-guides
#include <queue>
#include <list>
#include <iterator>
#include <cassert>
#include <cstddef>
int main()
{
// Test the explicit deduction guides
{
// queue(const Container&, const Alloc&);
// The '45' is not an allocator
std::queue que(std::list<int>{1,2,3}, 45); // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'queue'}}
}
{
// queue(const queue&, const Alloc&);
// The '45' is not an allocator
std::queue<int> source;
std::queue que(source, 45); // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'queue'}}
}
// Test the implicit deduction guides
{
// queue (allocator &)
std::queue que((std::allocator<int>())); // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'queue'}}
// Note: The extra parens are necessary, since otherwise clang decides it is a function declaration.
// Also, we can't use {} instead of parens, because that constructs a
// stack<allocator<int>, allocator<allocator<int>>>
}
}

View File

@ -0,0 +1,89 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <queue>
// UNSUPPORTED: c++98, c++03, c++11, c++14
// UNSUPPORTED: libcpp-no-deduction-guides
// template<class Container>
// queue(Container) -> queue<typename Container::value_type, Container>;
//
// template<class Container, class Allocator>
// queue(Container, Allocator) -> queue<typename Container::value_type, Container>;
#include <queue>
#include <list>
#include <iterator>
#include <cassert>
#include <cstddef>
#include <climits> // INT_MAX
#include "test_macros.h"
#include "test_iterators.h"
#include "test_allocator.h"
struct A {};
int main()
{
// Test the explicit deduction guides
{
std::list<int> l{0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std::queue que(l);
static_assert(std::is_same_v<decltype(que), std::queue<int, std::list<int>>>, "");
assert(que.size() == l.size());
assert(que.back() == l.back());
}
{
std::list<long, test_allocator<long>> l{10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
std::queue que(l, test_allocator<long>(0,2)); // different allocator
static_assert(std::is_same_v<decltype(que)::container_type, std::list<long, test_allocator<long>>>, "");
static_assert(std::is_same_v<decltype(que)::value_type, long>, "");
assert(que.size() == 10);
assert(que.back() == 19);
// I'd like to assert that we've gotten the right allocator in the queue, but
// I don't know how to get at the underlying container.
}
// Test the implicit deduction guides
{
// We don't expect this one to work - no way to implicitly get value_type
// std::queue que(std::allocator<int>()); // queue (allocator &)
}
{
std::queue<A> source;
std::queue que(source); // queue(queue &)
static_assert(std::is_same_v<decltype(que)::value_type, A>, "");
static_assert(std::is_same_v<decltype(que)::container_type, std::deque<A>>, "");
assert(que.size() == 0);
}
{
// This one is odd - you can pass an allocator in to use, but the allocator
// has to match the type of the one used by the underlying container
typedef short T;
typedef test_allocator<T> A;
typedef std::deque<T, A> C;
C c{0,1,2,3};
std::queue<T, C> source(c);
std::queue que(source, A(2)); // queue(queue &, allocator)
static_assert(std::is_same_v<decltype(que)::value_type, T>, "");
static_assert(std::is_same_v<decltype(que)::container_type, C>, "");
assert(que.size() == 4);
assert(que.back() == 3);
}
}

View File

@ -0,0 +1,53 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <stack>
// UNSUPPORTED: c++98, c++03, c++11, c++14
// UNSUPPORTED: libcpp-no-deduction-guides
// template <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
// vector(InputIterator, InputIterator, Allocator = Allocator())
// -> vector<typename iterator_traits<InputIterator>::value_type, Allocator>;
//
#include <stack>
#include <list>
#include <iterator>
#include <cassert>
#include <cstddef>
int main()
{
// Test the explicit deduction guides
{
// stack(const Container&, const Alloc&);
// The '45' is not an allocator
std::stack stk(std::list<int>({1,2,3}), 45); // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'stack'}}
}
{
// stack(const stack&, const Alloc&);
// The '45' is not an allocator
std::stack<int> source;
std::stack stk(source, 45); // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'stack'}}
}
// Test the implicit deduction guides
{
// stack (allocator &)
std::stack stk((std::allocator<int>())); // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'stack'}}
// Note: The extra parens are necessary, since otherwise clang decides it is a function declaration.
// Also, we can't use {} instead of parens, because that constructs a
// stack<allocator<int>, allocator<allocator<int>>>
}
}

View File

@ -0,0 +1,91 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <stack>
// UNSUPPORTED: c++98, c++03, c++11, c++14
// UNSUPPORTED: libcpp-no-deduction-guides
// template<class Container>
// stack(Container) -> stack<typename Container::value_type, Container>;
//
// template<class Container, class Allocator>
// stack(Container, Allocator) -> stack<typename Container::value_type, Container>;
#include <stack>
#include <vector>
#include <list>
#include <iterator>
#include <cassert>
#include <cstddef>
#include <climits> // INT_MAX
#include "test_macros.h"
#include "test_iterators.h"
#include "test_allocator.h"
struct A {};
int main()
{
// Test the explicit deduction guides
{
std::vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std::stack stk(v);
static_assert(std::is_same_v<decltype(stk), std::stack<int, std::vector<int>>>, "");
assert(stk.size() == v.size());
assert(stk.top() == v.back());
}
{
std::list<long, test_allocator<long>> l{10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
std::stack stk(l, test_allocator<long>(0,2)); // different allocator
static_assert(std::is_same_v<decltype(stk)::container_type, std::list<long, test_allocator<long>>>, "");
static_assert(std::is_same_v<decltype(stk)::value_type, long>, "");
assert(stk.size() == 10);
assert(stk.top() == 19);
// I'd like to assert that we've gotten the right allocator in the stack, but
// I don't know how to get at the underlying container.
}
// Test the implicit deduction guides
{
// We don't expect this one to work - no way to implicitly get value_type
// std::stack stk(std::allocator<int>()); // stack (allocator &)
}
{
std::stack<A> source;
std::stack stk(source); // stack(stack &)
static_assert(std::is_same_v<decltype(stk)::value_type, A>, "");
static_assert(std::is_same_v<decltype(stk)::container_type, std::deque<A>>, "");
assert(stk.size() == 0);
}
{
// This one is odd - you can pass an allocator in to use, but the allocator
// has to match the type of the one used by the underlying container
typedef short T;
typedef test_allocator<T> A;
typedef std::deque<T, A> C;
C c{0,1,2,3};
std::stack<T, C> source(c);
std::stack stk(source, A(2)); // stack(stack &, allocator)
static_assert(std::is_same_v<decltype(stk)::value_type, T>, "");
static_assert(std::is_same_v<decltype(stk)::container_type, C>, "");
assert(stk.size() == 4);
assert(stk.top() == 3);
}
}