From bfef3a711a4ee9cd649f03555ffa2764e4e33d54 Mon Sep 17 00:00:00 2001 From: Benjamin Kosnik Date: Thu, 21 Jul 2011 05:48:03 +0000 Subject: [PATCH] array (array::at, [...]): Mark constexpr. 2011-07-20 Benjamin Kosnik Daniel Krugler * include/std/array (array::at, array::operator[]): Mark constexpr. * testsuite/23_containers/array/requirements/ constexpr_element_access.cc: Add. Co-Authored-By: Daniel Krugler From-SVN: r176550 --- libstdc++-v3/ChangeLog | 9 +++++- libstdc++-v3/include/std/array | 16 ++++++---- .../requirements/constexpr_element_access.cc | 31 +++++++++++++++++++ 3 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 libstdc++-v3/testsuite/23_containers/array/requirements/constexpr_element_access.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index ec1897ca4cd..ae002030156 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,9 +1,16 @@ +2011-07-20 Benjamin Kosnik + Daniel Krugler + + * include/std/array (array::at, array::operator[]): Mark constexpr. + * testsuite/23_containers/array/requirements/ + constexpr_element_access.cc: Add. + 2011-07-20 Benjamin Kosnik Daniel Krugler * include/std/chrono: (system_clock::is_steady): Update to N3291 from is_monotonic. - (time_point): Add constexpr to nonmember arithmetic operators. + (time_point): Mark nonmember arithmetic operators constexpr. * src/chrono.cc: Modify for above. * src/compatibility-c++0x.cc: Same. * testsuite/20_util/time_point/nonmember/constexpr.cc: New. diff --git a/libstdc++-v3/include/std/array b/libstdc++-v3/include/std/array index 474b884ecb5..0abb628aded 100644 --- a/libstdc++-v3/include/std/array +++ b/libstdc++-v3/include/std/array @@ -35,6 +35,7 @@ # include #else +#include #include #include @@ -150,8 +151,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION operator[](size_type __n) { return _M_instance[__n]; } - const_reference - operator[](size_type __n) const + constexpr const_reference + operator[](size_type __n) const noexcept { return _M_instance[__n]; } reference @@ -162,12 +163,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return _M_instance[__n]; } - const_reference + constexpr const_reference at(size_type __n) const { - if (__n >= _Nm) - std::__throw_out_of_range(__N("array::at")); - return _M_instance[__n]; + return __n < _Nm ? _M_instance[__n] : +#ifdef __EXCEPTIONS + throw out_of_range(__N("array::at")); +#else + _M_instance[0]; +#endif } reference diff --git a/libstdc++-v3/testsuite/23_containers/array/requirements/constexpr_element_access.cc b/libstdc++-v3/testsuite/23_containers/array/requirements/constexpr_element_access.cc new file mode 100644 index 00000000000..23bc104aeab --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/array/requirements/constexpr_element_access.cc @@ -0,0 +1,31 @@ +// { dg-do compile } +// { dg-options "-std=gnu++0x" } + +// Copyright (C) 2011 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 +// . + +#include + +int main() +{ + // array + typedef std::array array_type; + constexpr array_type a = { 0, 55, 66, 99, 4115, 2 }; + constexpr auto v1 = a[1]; + constexpr auto v2 = a.at(2); + return 0; +} -- 2.30.2