From patchwork Thu Jul 21 04:43:23 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Kosnik X-Patchwork-Id: 105898 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 0775CB6F76 for ; Thu, 21 Jul 2011 14:43:54 +1000 (EST) Received: (qmail 13427 invoked by alias); 21 Jul 2011 04:43:47 -0000 Received: (qmail 13407 invoked by uid 22791); 21 Jul 2011 04:43:45 -0000 X-SWARE-Spam-Status: No, hits=-6.5 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, SPF_HELO_PASS X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 21 Jul 2011 04:43:25 +0000 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p6L4hOmI018723 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 21 Jul 2011 00:43:25 -0400 Received: from shotwell (ovpn-113-130.phx2.redhat.com [10.3.113.130]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p6L4hNpZ030713 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NO); Thu, 21 Jul 2011 00:43:24 -0400 Date: Wed, 20 Jul 2011 21:43:23 -0700 From: Benjamin Kosnik To: gcc-patches@gcc.gnu.org Cc: libstdc++@gcc.gnu.org, Daniel =?UTF-8?B?S3LDvGdsZXI=?= Subject: [v3] constexpr array additions Message-ID: <20110720214323.0d0d8a3e@shotwell> Mime-Version: 1.0 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org 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 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. Index: include/std/array =================================================================== --- include/std/array (revision 176549) +++ include/std/array (working copy) @@ -35,6 +35,7 @@ # include #else +#include #include #include @@ -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 +// . + +#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; +}