Deduction guides for list

llvm-svn: 332818
This commit is contained in:
Marshall Clow 2018-05-20 14:05:31 +00:00
parent da31c93794
commit 4a227e582b
3 changed files with 169 additions and 2 deletions

View File

@ -147,6 +147,11 @@ public:
void reverse() noexcept; void reverse() noexcept;
}; };
template <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
list(InputIterator, InputIterator, Allocator = Allocator())
-> list<typename iterator_traits<InputIterator>::value_type, Allocator>; // C++17
template <class T, class Alloc> template <class T, class Alloc>
bool operator==(const list<T,Alloc>& x, const list<T,Alloc>& y); bool operator==(const list<T,Alloc>& x, const list<T,Alloc>& y);
template <class T, class Alloc> template <class T, class Alloc>
@ -527,11 +532,12 @@ class __list_imp
{ {
__list_imp(const __list_imp&); __list_imp(const __list_imp&);
__list_imp& operator=(const __list_imp&); __list_imp& operator=(const __list_imp&);
protected: public:
typedef _Tp value_type;
typedef _Alloc allocator_type; typedef _Alloc allocator_type;
typedef allocator_traits<allocator_type> __alloc_traits; typedef allocator_traits<allocator_type> __alloc_traits;
typedef typename __alloc_traits::size_type size_type; typedef typename __alloc_traits::size_type size_type;
protected:
typedef _Tp value_type;
typedef typename __alloc_traits::void_pointer __void_pointer; typedef typename __alloc_traits::void_pointer __void_pointer;
typedef __list_iterator<value_type, __void_pointer> iterator; typedef __list_iterator<value_type, __void_pointer> iterator;
typedef __list_const_iterator<value_type, __void_pointer> const_iterator; typedef __list_const_iterator<value_type, __void_pointer> const_iterator;
@ -1106,6 +1112,22 @@ private:
void __move_assign(list& __c, false_type); void __move_assign(list& __c, false_type);
}; };
#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
template<class _InputIterator,
class _Alloc = typename std::allocator<typename iterator_traits<_InputIterator>::value_type>,
class = typename enable_if<__is_allocator<_Alloc>::value, void>::type
>
list(_InputIterator, _InputIterator)
-> list<typename iterator_traits<_InputIterator>::value_type, _Alloc>;
template<class _InputIterator,
class _Alloc,
class = typename enable_if<__is_allocator<_Alloc>::value, void>::type
>
list(_InputIterator, _InputIterator, _Alloc)
-> list<typename iterator_traits<_InputIterator>::value_type, _Alloc>;
#endif
// Link in nodes [__f, __l] just prior to __p // Link in nodes [__f, __l] just prior to __p
template <class _Tp, class _Alloc> template <class _Tp, class _Alloc>
inline inline

View File

@ -0,0 +1,42 @@
//===----------------------------------------------------------------------===//
//
// 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.
//
//===----------------------------------------------------------------------===//
// <list>
// 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>>
// list(InputIterator, InputIterator, Allocator = Allocator())
// -> list<typename iterator_traits<InputIterator>::value_type, Allocator>;
//
#include <list>
#include <iterator>
#include <cassert>
#include <cstddef>
#include <climits> // INT_MAX
struct A {};
int main()
{
// Test the explicit deduction guides
// Test the implicit deduction guides
{
// list (allocator &)
std::list lst((std::allocator<int>())); // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'list'}}
// 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
// deque<allocator<int>, allocator<allocator<int>>>
}
}

View File

@ -0,0 +1,103 @@
//===----------------------------------------------------------------------===//
//
// 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.
//
//===----------------------------------------------------------------------===//
// <list>
// 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>>
// list(InputIterator, InputIterator, Allocator = Allocator())
// -> list<typename iterator_traits<InputIterator>::value_type, Allocator>;
//
#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
{
const int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std::list lst(std::begin(arr), std::end(arr));
static_assert(std::is_same_v<decltype(lst), std::list<int>>, "");
assert(std::equal(lst.begin(), lst.end(), std::begin(arr), std::end(arr)));
}
{
const long arr[] = {INT_MAX, 1L, 2L, 3L };
std::list lst(std::begin(arr), std::end(arr), std::allocator<long>());
static_assert(std::is_same_v<decltype(lst)::value_type, long>, "");
assert(lst.size() == 4);
auto it = lst.begin();
assert(*it++ == INT_MAX);
assert(*it++ == 1L);
assert(*it++ == 2L);
}
// Test the implicit deduction guides
{
// We don't expect this one to work.
// std::list lst(std::allocator<int>()); // list (allocator &)
}
{
std::list lst(1, A{}); // list (size_type, T)
static_assert(std::is_same_v<decltype(lst)::value_type, A>, "");
static_assert(std::is_same_v<decltype(lst)::allocator_type, std::allocator<A>>, "");
assert(lst.size() == 1);
}
{
std::list lst(1, A{}, test_allocator<A>()); // list (size_type, T, allocator)
static_assert(std::is_same_v<decltype(lst)::value_type, A>, "");
static_assert(std::is_same_v<decltype(lst)::allocator_type, test_allocator<A>>, "");
assert(lst.size() == 1);
}
{
std::list lst{1U, 2U, 3U, 4U, 5U}; // list(initializer-list)
static_assert(std::is_same_v<decltype(lst)::value_type, unsigned>, "");
assert(lst.size() == 5);
auto it = lst.begin();
std::advance(it, 2);
assert(*it == 3U);
}
{
std::list lst({1.0, 2.0, 3.0, 4.0}, test_allocator<double>()); // list(initializer-list, allocator)
static_assert(std::is_same_v<decltype(lst)::value_type, double>, "");
static_assert(std::is_same_v<decltype(lst)::allocator_type, test_allocator<double>>, "");
assert(lst.size() == 4);
auto it = lst.begin();
std::advance(it, 3);
assert(*it == 4.0);
}
{
std::list<long double> source;
std::list lst(source); // list(list &)
static_assert(std::is_same_v<decltype(lst)::value_type, long double>, "");
static_assert(std::is_same_v<decltype(lst)::allocator_type, std::allocator<long double>>, "");
assert(lst.size() == 0);
}
}