Fix libstdc++/54388

Jonathan Wakely jwakely.gcc@gmail.com
Sun Sep 9 17:57:00 GMT 2012


        PR libstdc++/54388
        * include/std/array (array::at() const): Ensure lvalue result.
        * testsuite/23_containers/array/element_access/54388.cc: New.
        * 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.

Tested x86_64-linux, committed to trunk, will also commit on the 4.7 branch.
-------------- next part --------------
commit d44580f30981e08b492030451ad3ddb90541476f
Author: Jonathan Wakely <jwakely.gcc@gmail.com>
Date:   Sat Sep 8 11:19:15 2012 +0100

    	PR libstdc++/54388
    	* include/std/array (array::at() const): Ensure lvalue result.
    	* testsuite/23_containers/array/element_access/54388.cc: New.
    	* 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.

diff --git a/libstdc++-v3/include/std/array b/libstdc++-v3/include/std/array
index 58263ce..4ee2199 100644
--- a/libstdc++-v3/include/std/array
+++ b/libstdc++-v3/include/std/array
@@ -164,22 +164,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	return _M_instance[__n];
       }
 
-#ifdef __EXCEPTIONS
       constexpr const_reference
       at(size_type __n) const
       {
-	return __n < _Nm ? 
-	       _M_instance[__n] : throw out_of_range(__N("array::at"));
+	// 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]);
       }
-#else
-      const_reference
-      at(size_type __n) const
-      {
-	if (__n >= _Nm)
-	  std::__throw_out_of_range(__N("array::at"));
-	return _M_instance[__n];
-      }
-#endif
 
       reference 
       front()
diff --git a/libstdc++-v3/testsuite/23_containers/array/element_access/54388.cc b/libstdc++-v3/testsuite/23_containers/array/element_access/54388.cc
new file mode 100644
index 0000000..a69a5ed
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/array/element_access/54388.cc
@@ -0,0 +1,42 @@
+// { dg-options "-std=gnu++0x" }
+//
+// 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 <testsuite_hooks.h>
+
+struct A
+{
+  bool valid = true;
+  ~A() { valid = false; }
+};
+
+void
+test01()
+{
+  bool test __attribute__((unused)) = true;
+
+  const std::array<A, 1> a;
+  const A& aa = a.at(0);
+  VERIFY(aa.valid);
+}
+
+int main()
+{
+  test01();
+}
diff --git a/libstdc++-v3/testsuite/23_containers/array/tuple_interface/get_neg.cc b/libstdc++-v3/testsuite/23_containers/array/tuple_interface/get_neg.cc
index ecc25bf..2fc443e 100644
--- a/libstdc++-v3/testsuite/23_containers/array/tuple_interface/get_neg.cc
+++ b/libstdc++-v3/testsuite/23_containers/array/tuple_interface/get_neg.cc
@@ -29,4 +29,4 @@ 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 *-*-* } 291 }
+// { dg-error "static assertion failed" "" { target *-*-* } 267 }
diff --git a/libstdc++-v3/testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc b/libstdc++-v3/testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc
index 7263813..97938ba 100644
--- a/libstdc++-v3/testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc
+++ b/libstdc++-v3/testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc
@@ -22,4 +22,4 @@
 
 typedef std::tuple_element<1, std::array<int, 1>>::type type;
 
-// { dg-error "static assertion failed" "" { target *-*-* } 267 }
+// { dg-error "static assertion failed" "" { target *-*-* } 259 }


More information about the Libstdc++ mailing list