OSDN Git Service

PR libstdc++/54388
authorredi <redi@138bc75d-0d04-0410-961f-82ee72b054a4>
Sun, 9 Sep 2012 18:40:46 +0000 (18:40 +0000)
committerredi <redi@138bc75d-0d04-0410-961f-82ee72b054a4>
Sun, 9 Sep 2012 18:40:46 +0000 (18:40 +0000)
* include/std/array (array::at() const): Ensure lvalue result.
* testsuite/23_containers/array/element_access/54388.cc: New.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/branches/gcc-4_7-branch@191117 138bc75d-0d04-0410-961f-82ee72b054a4

libstdc++-v3/ChangeLog
libstdc++-v3/include/std/array
libstdc++-v3/testsuite/23_containers/array/element_access/54388.cc [new file with mode: 0644]

index 1e82730..cb4aa52 100644 (file)
@@ -1,3 +1,9 @@
+2012-09-09  Jonathan Wakely  <jwakely.gcc@gmail.com>
+
+       PR libstdc++/54388
+       * include/std/array (array::at() const): Ensure lvalue result.
+       * testsuite/23_containers/array/element_access/54388.cc: New.
+
 2012-09-06  Marc Glisse  <marc.glisse@inria.fr>
            Paolo Carlini  <paolo.carlini@oracle.com>
 
index 8cd5388..54cb98d 100644 (file)
@@ -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 (file)
index 0000000..a69a5ed
--- /dev/null
@@ -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();
+}