* [v3] constexpr array additions
@ 2011-07-21 7:11 Benjamin Kosnik
0 siblings, 0 replies; only message in thread
From: Benjamin Kosnik @ 2011-07-21 7:11 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Daniel Krügler
[-- Attachment #1: Type: text/plain, Size: 327 bytes --]
Also pointed out by Daniel.
It's kind of hokey to work around -fno-exceptions like this, but seems
harmless. In this particular case, deferring the throw to a function
like __throw_out_of_range makes the ::at member an invalid constant
expression.
Yay! std::array with compile time operator []!
tested x86/linux
-benjamin
[-- Attachment #2: 20110720-4.patch --]
[-- Type: text/x-patch, Size: 2717 bytes --]
2011-07-20 Benjamin Kosnik <bkoz@redhat.com>
Daniel Krugler <daniel.kruegler@googlemail.com>
* include/std/array (array::at, array::operator[]): Mark constexpr.
* testsuite/23_containers/array/requirements/
constexpr_element_access.cc: Add.
Index: include/std/array
===================================================================
--- include/std/array (revision 176549)
+++ include/std/array (working copy)
@@ -35,6 +35,7 @@
# include <bits/c++0x_warning.h>
#else
+#include <stdexcept>
#include <bits/stl_algobase.h>
#include <bits/range_access.h>
@@ -150,8 +151,8 @@
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 @@
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
Index: testsuite/23_containers/array/requirements/constexpr_element_access.cc
===================================================================
--- testsuite/23_containers/array/requirements/constexpr_element_access.cc (revision 0)
+++ testsuite/23_containers/array/requirements/constexpr_element_access.cc (revision 0)
@@ -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
+// <http://www.gnu.org/licenses/>.
+
+#include <array>
+
+int main()
+{
+ // array
+ typedef std::array<std::size_t, 6> 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;
+}
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2011-07-21 4:43 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-21 7:11 [v3] constexpr array additions Benjamin Kosnik
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).