[v3] libstdc++/53248

Paolo Carlini paolo.carlini@oracle.com
Wed Oct 3 16:25:00 GMT 2012


Hi,

thus this is the patch I prepared for this issue, consistently (I hope!) 
with the various points discussed in the audit trail, with Daniel too. 
In particular: 1- We'd rather prefer not use a specialization; 2- Having 
begin() and end() always returning nullptr for zero-sized-arrays is fine.

Of course the patch could be tweaked in various different ways but if 
nobody has special suggestions this is the version I'm going to commit.

Tested x86_64-linux.

Thanks!
Paolo.

////////////////////////
-------------- next part --------------
2012-10-03  Paolo Carlini  <paolo.carlini@oracle.com>

	PR libstdc++/53248
	* include/std/array (__array_elems<>): Add.
	(array<>): Allow for zero-size arrays of non default-constructible
	elements.
	* testsuite/23_containers/array/requirements/
	non_default_constructible.cc: New.
	* testsuite/23_containers/array/requirements/zero_sized_arrays.cc:
	Adjust.
	* testsuite/23_containers/array/tuple_interface/get_neg.cc: Adjust
	dg-error line numbers.
	* testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc:
	Likewise.
-------------- next part --------------
Index: include/std/array
===================================================================
--- include/std/array	(revision 192031)
+++ include/std/array	(working copy)
@@ -1,7 +1,6 @@
 // <array> -*- C++ -*-
 
-// Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012
-// Free Software Foundation, Inc.
+// Copyright (C) 2007-2012 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
@@ -44,6 +43,24 @@
 {
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
+  template<typename _Tp, std::size_t _Nm>
+    struct __array_elems
+    {
+      _Tp _Array[_Nm];
+
+      constexpr _Tp&
+      _M_ref(std::size_t __n) noexcept
+      { return const_cast<_Tp&>(_Array[__n]); }
+   };
+
+ template<typename _Tp>
+   struct __array_elems<_Tp, 0>
+   {
+     constexpr _Tp&
+     _M_ref(std::size_t) noexcept
+     { return *static_cast<_Tp*>(nullptr); }
+   };
+
   /**
    *  @brief A standard container for storing a fixed size sequence of elements.
    *
@@ -74,7 +91,7 @@
       typedef std::reverse_iterator<const_iterator>   const_reverse_iterator;
 
       // Support for zero-sized arrays mandatory.
-      value_type _M_instance[_Nm ? _Nm : 1];
+      typename std::__array_elems<_Tp, _Nm>           _M_elems;
 
       // No explicit construct/copy/destroy for aggregate type.
 
@@ -123,11 +140,11 @@
 
       const_iterator
       cbegin() const noexcept
-      { return const_iterator(std::__addressof(_M_instance[0])); }
+      { return const_iterator(data()); }
 
       const_iterator
       cend() const noexcept
-      { return const_iterator(std::__addressof(_M_instance[_Nm])); }
+      { return const_iterator(data() + _Nm); }
 
       const_reverse_iterator 
       crbegin() const noexcept
@@ -150,18 +167,18 @@
       // Element access.
       reference
       operator[](size_type __n)
-      { return _M_instance[__n]; }
+      { return _M_elems._M_ref(__n); }
 
       constexpr const_reference
       operator[](size_type __n) const noexcept
-      { return _M_instance[__n]; }
+      { return _M_elems._M_ref(__n); }
 
       reference
       at(size_type __n)
       {
 	if (__n >= _Nm)
 	  std::__throw_out_of_range(__N("array::at"));
-	return _M_instance[__n];
+	return _M_elems._M_ref(__n);
       }
 
       constexpr const_reference
@@ -169,8 +186,8 @@
       {
 	// Result of conditional expression must be an lvalue so use
 	// boolean ? lvalue : (throw-expr, lvalue)
-	return __n < _Nm ? _M_instance[__n]
-	  : (std::__throw_out_of_range(__N("array::at")), _M_instance[0]);
+	return __n < _Nm ? _M_elems._M_ref(__n)
+	  : (std::__throw_out_of_range(__N("array::at")), _M_elems._M_ref(0));
       }
 
       reference 
@@ -191,11 +208,11 @@
 
       pointer
       data() noexcept
-      { return std::__addressof(_M_instance[0]); }
+      { return std::__addressof(_M_elems._M_ref(0)); }
 
       const_pointer
       data() const noexcept
-      { return std::__addressof(_M_instance[0]); }
+      { return std::__addressof(_M_elems._M_ref(0)); }
     };
 
   // Array comparisons.
@@ -265,7 +282,7 @@
     get(array<_Tp, _Nm>& __arr) noexcept
     {
       static_assert(_Int < _Nm, "index is out of bounds");
-      return __arr._M_instance[_Int];
+      return __arr._M_elems._M_ref(_Int);
     }
 
   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
@@ -281,7 +298,7 @@
     get(const array<_Tp, _Nm>& __arr) noexcept
     {
       static_assert(_Int < _Nm, "index is out of bounds");
-      return __arr._M_instance[_Int];
+      return __arr._M_elems._M_ref(_Int);
     }
 
 _GLIBCXX_END_NAMESPACE_VERSION
Index: testsuite/23_containers/array/tuple_interface/get_neg.cc
===================================================================
--- testsuite/23_containers/array/tuple_interface/get_neg.cc	(revision 192031)
+++ testsuite/23_containers/array/tuple_interface/get_neg.cc	(working copy)
@@ -27,6 +27,6 @@
 int n2 = std::get<1>(std::move(a));
 int n3 = std::get<1>(ca);
 
-// { dg-error "static assertion failed" "" { target *-*-* } 275 }
-// { dg-error "static assertion failed" "" { target *-*-* } 283 }
-// { dg-error "static assertion failed" "" { target *-*-* } 267 }
+// { dg-error "static assertion failed" "" { target *-*-* } 284 }
+// { dg-error "static assertion failed" "" { target *-*-* } 292 }
+// { dg-error "static assertion failed" "" { target *-*-* } 300 }
Index: testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc
===================================================================
--- testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc	(revision 192031)
+++ testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc	(working copy)
@@ -22,4 +22,4 @@
 
 typedef std::tuple_element<1, std::array<int, 1>>::type type;
 
-// { dg-error "static assertion failed" "" { target *-*-* } 259 }
+// { dg-error "static assertion failed" "" { target *-*-* } 276 }
Index: testsuite/23_containers/array/requirements/zero_sized_arrays.cc
===================================================================
--- testsuite/23_containers/array/requirements/zero_sized_arrays.cc	(revision 192031)
+++ testsuite/23_containers/array/requirements/zero_sized_arrays.cc	(working copy)
@@ -1,6 +1,6 @@
 // { dg-options "-std=gnu++0x" }
 //
-// Copyright (C) 2011 Free Software Foundation, Inc.
+// Copyright (C) 2011-2012 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
@@ -38,18 +38,6 @@
   // begin() == end()
   VERIFY( a.begin() == a.end() );
   VERIFY( b.begin() == b.end() );
-
-  // 4: ?
-  // begin() == end() == unique value.
-  {
-    typedef std::array<long, len> array_type1;
-    typedef std::array<char, len> array_type2;
-    array_type1 one;
-    array_type2 two;
-    void* v1 = one.begin();
-    void* v2 = two.begin();
-    VERIFY( v1 != v2 );
-  }
 }
 
 int main()
Index: testsuite/23_containers/array/requirements/non_default_constructible.cc
===================================================================
--- testsuite/23_containers/array/requirements/non_default_constructible.cc	(revision 0)
+++ testsuite/23_containers/array/requirements/non_default_constructible.cc	(working copy)
@@ -0,0 +1,48 @@
+// { dg-options "-std=gnu++11" }
+// { dg-do compile }
+
+// Copyright (C) 2012 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/>.
+
+#include <array>
+#include <typeindex>
+#include <typeinfo>
+
+template < typename ...Types >
+union super_union;
+
+template < >
+union super_union<>
+{
+  static  auto optioned_types() -> std::array<std::type_index, 0>
+  { return std::array<std::type_index, 0>{ {} }; }
+};
+
+template < typename Head, typename ...Tail >
+union super_union<Head, Tail...>
+{
+  static
+  auto optioned_types() -> std::array<std::type_index, 1 + sizeof...(Tail)>
+  {
+    using std::type_index;
+
+    return { {type_index(typeid(Head)), type_index(typeid(Tail))...} };
+  }
+
+  Head                  data;
+  super_union<Tail...>  rest;
+};


More information about the Libstdc++ mailing list