[libcxx] r332818 - Deduction guides for list

Marshall Clow via cfe-commits cfe-commits at lists.llvm.org
Sun May 20 07:05:31 PDT 2018


Author: marshall
Date: Sun May 20 07:05:31 2018
New Revision: 332818

URL: http://llvm.org/viewvc/llvm-project?rev=332818&view=rev
Log:
Deduction guides for list

Added:
    libcxx/trunk/test/std/containers/sequences/list/list.cons/deduct.fail.cpp
    libcxx/trunk/test/std/containers/sequences/list/list.cons/deduct.pass.cpp
Modified:
    libcxx/trunk/include/list

Modified: libcxx/trunk/include/list
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/list?rev=332818&r1=332817&r2=332818&view=diff
==============================================================================
--- libcxx/trunk/include/list (original)
+++ libcxx/trunk/include/list Sun May 20 07:05:31 2018
@@ -147,6 +147,11 @@ public:
     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>
     bool operator==(const list<T,Alloc>& x, const list<T,Alloc>& y);
 template <class T, class Alloc>
@@ -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<allocator_type>                        __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<value_type, __void_pointer>             iterator;
     typedef __list_const_iterator<value_type, __void_pointer>       const_iterator;
@@ -1106,6 +1112,22 @@ private:
     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
 template <class _Tp, class _Alloc>
 inline

Added: libcxx/trunk/test/std/containers/sequences/list/list.cons/deduct.fail.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/sequences/list/list.cons/deduct.fail.cpp?rev=332818&view=auto
==============================================================================
--- libcxx/trunk/test/std/containers/sequences/list/list.cons/deduct.fail.cpp (added)
+++ libcxx/trunk/test/std/containers/sequences/list/list.cons/deduct.fail.cpp Sun May 20 07:05:31 2018
@@ -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>>>
+    }
+
+}

Added: libcxx/trunk/test/std/containers/sequences/list/list.cons/deduct.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/sequences/list/list.cons/deduct.pass.cpp?rev=332818&view=auto
==============================================================================
--- libcxx/trunk/test/std/containers/sequences/list/list.cons/deduct.pass.cpp (added)
+++ libcxx/trunk/test/std/containers/sequences/list/list.cons/deduct.pass.cpp Sun May 20 07:05:31 2018
@@ -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);
+    }
+}




More information about the cfe-commits mailing list