[PATCH] Add deduction guides for sequence containers (P0433R2, partial)

Jonathan Wakely jwakely@redhat.com
Fri Jun 9 11:05:00 GMT 2017


More deduction guides for C++17.

	* include/bits/forward_list.h (forward_list): Add deduction guide.
	* include/bits/stl_deque.h (deque): Likewise.
	* include/bits/stl_list.h (list): Likewise.
	* include/bits/stl_vector.h (vector): Likewise.
	* testsuite/23_containers/deque/cons/deduction.cc: New.
	* testsuite/23_containers/forward_list/cons/deduction.cc: New.
	* testsuite/23_containers/list/cons/deduction.cc: New.
	* testsuite/23_containers/vector/cons/deduction.cc: New.

Tested powerpc64le-linux, committed to trunk.

-------------- next part --------------
commit 5e7791167e0d3cd1af3e28336a0719d7d620ed70
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Jun 8 15:01:46 2017 +0100

    Add deduction guides for sequence containers (P0433R2, partial)
    
    	* include/bits/forward_list.h (forward_list): Add deduction guide.
    	* include/bits/stl_deque.h (deque): Likewise.
    	* include/bits/stl_list.h (list): Likewise.
    	* include/bits/stl_vector.h (vector): Likewise.
    	* testsuite/23_containers/deque/cons/deduction.cc: New.
    	* testsuite/23_containers/forward_list/cons/deduction.cc: New.
    	* testsuite/23_containers/list/cons/deduction.cc: New.
    	* testsuite/23_containers/vector/cons/deduction.cc: New.

diff --git a/libstdc++-v3/include/bits/forward_list.h b/libstdc++-v3/include/bits/forward_list.h
index c37bf01..f319b7f 100644
--- a/libstdc++-v3/include/bits/forward_list.h
+++ b/libstdc++-v3/include/bits/forward_list.h
@@ -1359,6 +1359,16 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       }
     };
 
+#if __cpp_deduction_guides >= 201606
+  template<typename _InputIterator, typename _ValT
+	     = typename iterator_traits<_InputIterator>::value_type,
+	   typename _Allocator = allocator<_ValT>,
+	   typename = _RequireInputIter<_InputIterator>,
+	   typename = _RequireAllocator<_Allocator>>
+    forward_list(_InputIterator, _InputIterator, _Allocator = _Allocator())
+      -> forward_list<_ValT, _Allocator>;
+#endif
+
   /**
    *  @brief  Forward list equality comparison.
    *  @param  __lx  A %forward_list
diff --git a/libstdc++-v3/include/bits/stl_deque.h b/libstdc++-v3/include/bits/stl_deque.h
index 6090635..e03e7f5 100644
--- a/libstdc++-v3/include/bits/stl_deque.h
+++ b/libstdc++-v3/include/bits/stl_deque.h
@@ -2242,6 +2242,15 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 #endif
     };
 
+#if __cpp_deduction_guides >= 201606
+  template<typename _InputIterator, typename _ValT
+	     = typename iterator_traits<_InputIterator>::value_type,
+	   typename _Allocator = allocator<_ValT>,
+	   typename = _RequireInputIter<_InputIterator>,
+	   typename = _RequireAllocator<_Allocator>>
+    deque(_InputIterator, _InputIterator, _Allocator = _Allocator())
+      -> deque<_ValT, _Allocator>;
+#endif
 
   /**
    *  @brief  Deque equality comparison.
diff --git a/libstdc++-v3/include/bits/stl_list.h b/libstdc++-v3/include/bits/stl_list.h
index 0420dbf..232885a 100644
--- a/libstdc++-v3/include/bits/stl_list.h
+++ b/libstdc++-v3/include/bits/stl_list.h
@@ -1867,6 +1867,17 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
       }
 #endif
     };
+
+#if __cpp_deduction_guides >= 201606
+  template<typename _InputIterator, typename _ValT
+	     = typename iterator_traits<_InputIterator>::value_type,
+	   typename _Allocator = allocator<_ValT>,
+	   typename = _RequireInputIter<_InputIterator>,
+	   typename = _RequireAllocator<_Allocator>>
+    list(_InputIterator, _InputIterator, _Allocator = _Allocator())
+      -> list<_ValT, _Allocator>;
+#endif
+
 _GLIBCXX_END_NAMESPACE_CXX11
 
   /**
diff --git a/libstdc++-v3/include/bits/stl_vector.h b/libstdc++-v3/include/bits/stl_vector.h
index fb88212..7ee3ce9 100644
--- a/libstdc++-v3/include/bits/stl_vector.h
+++ b/libstdc++-v3/include/bits/stl_vector.h
@@ -1580,6 +1580,15 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 #endif
     };
 
+#if __cpp_deduction_guides >= 201606
+  template<typename _InputIterator, typename _ValT
+	     = typename iterator_traits<_InputIterator>::value_type,
+	   typename _Allocator = allocator<_ValT>,
+	   typename = _RequireInputIter<_InputIterator>,
+	   typename = _RequireAllocator<_Allocator>>
+    vector(_InputIterator, _InputIterator, _Allocator = _Allocator())
+      -> vector<_ValT, _Allocator>;
+#endif
 
   /**
    *  @brief  Vector equality comparison.
diff --git a/libstdc++-v3/testsuite/23_containers/deque/cons/deduction.cc b/libstdc++-v3/testsuite/23_containers/deque/cons/deduction.cc
new file mode 100644
index 0000000..c7c0f2e
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/deque/cons/deduction.cc
@@ -0,0 +1,70 @@
+// Copyright (C) 2017 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++17" }
+// { dg-do compile { target c++1z } }
+
+#include <deque>
+#include <testsuite_iterators.h>
+
+template<typename T>
+  using input_iterator_seq
+    = __gnu_test::test_container<T, __gnu_test::input_iterator_wrapper>;
+
+template<typename T, typename U> struct require_same;
+template<typename T> struct require_same<T, T> { using type = void; };
+
+template<typename T, typename U>
+  typename require_same<T, U>::type
+  check_type(U&) { }
+
+void
+test01()
+{
+  std::deque<unsigned> s0;
+
+  std::deque s1 = s0;
+  check_type<std::deque<unsigned>>(s1);
+
+  std::deque s2 = std::move(s0);
+  check_type<std::deque<unsigned>>(s2);
+
+  const std::deque s3 = s0;
+  check_type<const std::deque<unsigned>>(s3);
+
+  const std::deque s4 = s3;
+  check_type<const std::deque<unsigned>>(s4);
+}
+
+void
+test02()
+{
+  unsigned a[1] = {};
+  input_iterator_seq<unsigned> seq(a);
+
+  std::deque s1(seq.begin(), seq.end());
+  check_type<std::deque<unsigned>>(s1);
+
+  std::deque s2(seq.begin(), seq.end(), std::allocator<unsigned>());
+  check_type<std::deque<unsigned>>(s2);
+
+  std::deque s3(1U, 2L);
+  check_type<std::deque<long>>(s3);
+
+  std::deque s4(1U, 2L, std::allocator<long>());
+  check_type<std::deque<long>>(s4);
+}
diff --git a/libstdc++-v3/testsuite/23_containers/forward_list/cons/deduction.cc b/libstdc++-v3/testsuite/23_containers/forward_list/cons/deduction.cc
new file mode 100644
index 0000000..ee30288
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/forward_list/cons/deduction.cc
@@ -0,0 +1,70 @@
+// Copyright (C) 2017 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++17" }
+// { dg-do compile { target c++1z } }
+
+#include <forward_list>
+#include <testsuite_iterators.h>
+
+template<typename T>
+  using input_iterator_seq
+    = __gnu_test::test_container<T, __gnu_test::input_iterator_wrapper>;
+
+template<typename T, typename U> struct require_same;
+template<typename T> struct require_same<T, T> { using type = void; };
+
+template<typename T, typename U>
+  typename require_same<T, U>::type
+  check_type(U&) { }
+
+void
+test01()
+{
+  std::forward_list<unsigned> s0;
+
+  std::forward_list s1 = s0;
+  check_type<std::forward_list<unsigned>>(s1);
+
+  std::forward_list s2 = std::move(s0);
+  check_type<std::forward_list<unsigned>>(s2);
+
+  const std::forward_list s3 = s0;
+  check_type<const std::forward_list<unsigned>>(s3);
+
+  const std::forward_list s4 = s3;
+  check_type<const std::forward_list<unsigned>>(s4);
+}
+
+void
+test02()
+{
+  unsigned a[1] = {};
+  input_iterator_seq<unsigned> seq(a);
+
+  std::forward_list s1(seq.begin(), seq.end());
+  check_type<std::forward_list<unsigned>>(s1);
+
+  std::forward_list s2(seq.begin(), seq.end(), std::allocator<unsigned>());
+  check_type<std::forward_list<unsigned>>(s2);
+
+  std::forward_list s3(1U, 2L);
+  check_type<std::forward_list<long>>(s3);
+
+  std::forward_list s4(1U, 2L, std::allocator<long>());
+  check_type<std::forward_list<long>>(s4);
+}
diff --git a/libstdc++-v3/testsuite/23_containers/list/cons/deduction.cc b/libstdc++-v3/testsuite/23_containers/list/cons/deduction.cc
new file mode 100644
index 0000000..c4df795
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/list/cons/deduction.cc
@@ -0,0 +1,70 @@
+// Copyright (C) 2017 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++17" }
+// { dg-do compile { target c++1z } }
+
+#include <list>
+#include <testsuite_iterators.h>
+
+template<typename T>
+  using input_iterator_seq
+    = __gnu_test::test_container<T, __gnu_test::input_iterator_wrapper>;
+
+template<typename T, typename U> struct require_same;
+template<typename T> struct require_same<T, T> { using type = void; };
+
+template<typename T, typename U>
+  typename require_same<T, U>::type
+  check_type(U&) { }
+
+void
+test01()
+{
+  std::list<unsigned> s0;
+
+  std::list s1 = s0;
+  check_type<std::list<unsigned>>(s1);
+
+  std::list s2 = std::move(s0);
+  check_type<std::list<unsigned>>(s2);
+
+  const std::list s3 = s0;
+  check_type<const std::list<unsigned>>(s3);
+
+  const std::list s4 = s3;
+  check_type<const std::list<unsigned>>(s4);
+}
+
+void
+test02()
+{
+  unsigned a[1] = {};
+  input_iterator_seq<unsigned> seq(a);
+
+  std::list s1(seq.begin(), seq.end());
+  check_type<std::list<unsigned>>(s1);
+
+  std::list s2(seq.begin(), seq.end(), std::allocator<unsigned>());
+  check_type<std::list<unsigned>>(s2);
+
+  std::list s3(1U, 2L);
+  check_type<std::list<long>>(s3);
+
+  std::list s4(1U, 2L, std::allocator<long>());
+  check_type<std::list<long>>(s4);
+}
diff --git a/libstdc++-v3/testsuite/23_containers/vector/cons/deduction.cc b/libstdc++-v3/testsuite/23_containers/vector/cons/deduction.cc
new file mode 100644
index 0000000..413f02c
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/vector/cons/deduction.cc
@@ -0,0 +1,70 @@
+// Copyright (C) 2017 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++17" }
+// { dg-do compile { target c++1z } }
+
+#include <vector>
+#include <testsuite_iterators.h>
+
+template<typename T>
+  using input_iterator_seq
+    = __gnu_test::test_container<T, __gnu_test::input_iterator_wrapper>;
+
+template<typename T, typename U> struct require_same;
+template<typename T> struct require_same<T, T> { using type = void; };
+
+template<typename T, typename U>
+  typename require_same<T, U>::type
+  check_type(U&) { }
+
+void
+test01()
+{
+  std::vector<unsigned> s0;
+
+  std::vector s1 = s0;
+  check_type<std::vector<unsigned>>(s1);
+
+  std::vector s2 = std::move(s0);
+  check_type<std::vector<unsigned>>(s2);
+
+  const std::vector s3 = s0;
+  check_type<const std::vector<unsigned>>(s3);
+
+  const std::vector s4 = s3;
+  check_type<const std::vector<unsigned>>(s4);
+}
+
+void
+test02()
+{
+  unsigned a[1] = {};
+  input_iterator_seq<unsigned> seq(a);
+
+  std::vector s1(seq.begin(), seq.end());
+  check_type<std::vector<unsigned>>(s1);
+
+  std::vector s2(seq.begin(), seq.end(), std::allocator<unsigned>());
+  check_type<std::vector<unsigned>>(s2);
+
+  std::vector s3(1U, 2L);
+  check_type<std::vector<long>>(s3);
+
+  std::vector s4(1U, 2L, std::allocator<long>());
+  check_type<std::vector<long>>(s4);
+}


More information about the Gcc-patches mailing list