From 4a227e582b2f13880ea049b29988a37a0f7c0742 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Sun, 20 May 2018 14:05:31 +0000 Subject: [PATCH] Deduction guides for list llvm-svn: 332818 --- libcxx/include/list | 26 ++++- .../sequences/list/list.cons/deduct.fail.cpp | 42 +++++++ .../sequences/list/list.cons/deduct.pass.cpp | 103 ++++++++++++++++++ 3 files changed, 169 insertions(+), 2 deletions(-) create mode 100644 libcxx/test/std/containers/sequences/list/list.cons/deduct.fail.cpp create mode 100644 libcxx/test/std/containers/sequences/list/list.cons/deduct.pass.cpp diff --git a/libcxx/include/list b/libcxx/include/list index f884b168126f..4b22017f713d 100644 --- a/libcxx/include/list +++ b/libcxx/include/list @@ -147,6 +147,11 @@ public: void reverse() noexcept; }; + +template ::value_type>> + list(InputIterator, InputIterator, Allocator = Allocator()) + -> list::value_type, Allocator>; // C++17 + template bool operator==(const list& x, const list& y); template @@ -527,11 +532,12 @@ class __list_imp { __list_imp(const __list_imp&); __list_imp& operator=(const __list_imp&); -protected: - typedef _Tp value_type; +public: typedef _Alloc allocator_type; typedef allocator_traits __alloc_traits; typedef typename __alloc_traits::size_type size_type; +protected: + typedef _Tp value_type; typedef typename __alloc_traits::void_pointer __void_pointer; typedef __list_iterator iterator; typedef __list_const_iterator const_iterator; @@ -1106,6 +1112,22 @@ private: void __move_assign(list& __c, false_type); }; +#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES +template::value_type>, + class = typename enable_if<__is_allocator<_Alloc>::value, void>::type + > +list(_InputIterator, _InputIterator) + -> list::value_type, _Alloc>; + +template::value, void>::type + > +list(_InputIterator, _InputIterator, _Alloc) + -> list::value_type, _Alloc>; +#endif + // Link in nodes [__f, __l] just prior to __p template inline diff --git a/libcxx/test/std/containers/sequences/list/list.cons/deduct.fail.cpp b/libcxx/test/std/containers/sequences/list/list.cons/deduct.fail.cpp new file mode 100644 index 000000000000..c3709b795d14 --- /dev/null +++ b/libcxx/test/std/containers/sequences/list/list.cons/deduct.fail.cpp @@ -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. +// +//===----------------------------------------------------------------------===// + +// +// UNSUPPORTED: c++98, c++03, c++11, c++14 +// UNSUPPORTED: libcpp-no-deduction-guides + + +// template ::value_type>> +// list(InputIterator, InputIterator, Allocator = Allocator()) +// -> list::value_type, Allocator>; +// + + +#include +#include +#include +#include +#include // INT_MAX + +struct A {}; + +int main() +{ +// Test the explicit deduction guides + +// Test the implicit deduction guides + { +// list (allocator &) + std::list lst((std::allocator())); // 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>> + } + +} diff --git a/libcxx/test/std/containers/sequences/list/list.cons/deduct.pass.cpp b/libcxx/test/std/containers/sequences/list/list.cons/deduct.pass.cpp new file mode 100644 index 000000000000..66aa37acc835 --- /dev/null +++ b/libcxx/test/std/containers/sequences/list/list.cons/deduct.pass.cpp @@ -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. +// +//===----------------------------------------------------------------------===// + +// +// UNSUPPORTED: c++98, c++03, c++11, c++14 +// UNSUPPORTED: libcpp-no-deduction-guides + + +// template ::value_type>> +// list(InputIterator, InputIterator, Allocator = Allocator()) +// -> list::value_type, Allocator>; +// + + +#include +#include +#include +#include +#include // 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>, ""); + 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()); + static_assert(std::is_same_v, ""); + 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()); // list (allocator &) + } + + { + std::list lst(1, A{}); // list (size_type, T) + static_assert(std::is_same_v, ""); + static_assert(std::is_same_v>, ""); + assert(lst.size() == 1); + } + + { + std::list lst(1, A{}, test_allocator()); // list (size_type, T, allocator) + static_assert(std::is_same_v, ""); + static_assert(std::is_same_v>, ""); + assert(lst.size() == 1); + } + + { + std::list lst{1U, 2U, 3U, 4U, 5U}; // list(initializer-list) + static_assert(std::is_same_v, ""); + 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()); // list(initializer-list, allocator) + static_assert(std::is_same_v, ""); + static_assert(std::is_same_v>, ""); + assert(lst.size() == 4); + auto it = lst.begin(); + std::advance(it, 3); + assert(*it == 4.0); + } + + { + std::list source; + std::list lst(source); // list(list &) + static_assert(std::is_same_v, ""); + static_assert(std::is_same_v>, ""); + assert(lst.size() == 0); + } +}