From patchwork Thu Oct 31 19:41:55 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 287594 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]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id A16A52C03E8 for ; Fri, 1 Nov 2013 06:42:09 +1100 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:date:message-id:subject:from:to:content-type; q= dns; s=default; b=Efj+ZMrYCscIWxz1tyOWy+/UnlOFn8PXb1sC/Kzy0+CB6m YkY76X32RBIby3YfakX23+V93fA4RbRJccP0XQ6+4/CDynnYk/Nn+iQYdsFywHtl 80wfmZUhNc4F0KUu5aVaki6k+0Apr0iQkckALcA5lVWcGO8NhO6fZ4EVToSuE= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:date:message-id:subject:from:to:content-type; s= default; bh=C9QQsO6oYkvECz6TmUKx3JJ3MoA=; b=ThIdWGjHSIJB3iwZDdaJ RLRO7xvuZDenuwCfAZj1pEYrwIk3Iu3SWq7Fzt1n1Pa8Q6V6FVNm9VPRH9blIJnu IrXQ0YCYCjv1DsHP0hLxEinEs2g+grVS7uZF9rLQEiSws9sDxOahb1d6wJ93JOqk FqT8ALdx49QHUe3G2Y9Oqns= Received: (qmail 25585 invoked by alias); 31 Oct 2013 19:42:02 -0000 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 Received: (qmail 25563 invoked by uid 89); 31 Oct 2013 19:42:01 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.8 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 X-Spam-User: qpsmtpd, 2 recipients X-HELO: mail-la0-f44.google.com Received: from mail-la0-f44.google.com (HELO mail-la0-f44.google.com) (209.85.215.44) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Thu, 31 Oct 2013 19:42:00 +0000 Received: by mail-la0-f44.google.com with SMTP id ep20so2712967lab.31 for ; Thu, 31 Oct 2013 12:41:56 -0700 (PDT) MIME-Version: 1.0 X-Received: by 10.152.3.42 with SMTP id 10mr3154147laz.22.1383248516258; Thu, 31 Oct 2013 12:41:56 -0700 (PDT) Received: by 10.112.101.7 with HTTP; Thu, 31 Oct 2013 12:41:55 -0700 (PDT) Date: Thu, 31 Oct 2013 19:41:55 +0000 Message-ID: Subject: [v3 patch] define std::integer_sequence et al From: Jonathan Wakely To: "libstdc++" , gcc-patches Implement my N3658 proposal for C++14. I moved the existing _Build_index_tuple from to and re-used it for std::make_integer_sequence (so that if G++ one day provides an intrinsic to generate a parameter pack of integers [0, N) we only need to change one place to use it.) 2013-10-31 Jonathan Wakely * include/std/tuple (_Index_tuple, _Build_index_tuple): Move to . * include/std/utility (integer_sequence, make_integer_sequence, index_sequence, make_index_sequence, index_sequence_for): Define. * doc/xml/manual/status_cxx2014.xml: Update. * testsuite/20_util/integer_sequence/intseq.cc: New. * testsuite/20_util/integer_sequence/requirements/typedefs.cc: New. Tested x86_64-linux, committed to trunk. commit 29a1079a3edcb51833cf0739affb2d9b3a59d4c3 Author: Jonathan Wakely Date: Sun Apr 28 13:59:42 2013 +0100 * include/std/tuple (_Index_tuple, _Build_index_tuple): Move to . * include/std/utility (integer_sequence, make_integer_sequence, index_sequence, make_index_sequence, index_sequence_for): Define. * doc/xml/manual/status_cxx2014.xml: Update. * testsuite/20_util/integer_sequence/intseq.cc: New. * testsuite/20_util/integer_sequence/requirements/typedefs.cc: New. diff --git a/libstdc++-v3/doc/xml/manual/status_cxx2014.xml b/libstdc++-v3/doc/xml/manual/status_cxx2014.xml index 1f20f0e..4ef4334 100644 --- a/libstdc++-v3/doc/xml/manual/status_cxx2014.xml +++ b/libstdc++-v3/doc/xml/manual/status_cxx2014.xml @@ -185,8 +185,8 @@ particular release. Compile-time integer sequences - WIP - Need tests + Y + diff --git a/libstdc++-v3/include/std/tuple b/libstdc++-v3/include/std/tuple index 063ce02..2580f78 100644 --- a/libstdc++-v3/include/std/tuple +++ b/libstdc++-v3/include/std/tuple @@ -917,27 +917,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ::type>::type>::type { }; - // Stores a tuple of indices. Also used by bind() to extract the elements - // in a tuple. - template - struct _Index_tuple - { - typedef _Index_tuple<_Indexes..., sizeof...(_Indexes)> __next; - }; - - // Builds an _Index_tuple<0, 1, 2, ..., _Num-1>. - template - struct _Build_index_tuple - { - typedef typename _Build_index_tuple<_Num - 1>::__type::__next __type; - }; - - template<> - struct _Build_index_tuple<0> - { - typedef _Index_tuple<> __type; - }; - template struct __make_tuple_impl; diff --git a/libstdc++-v3/include/std/utility b/libstdc++-v3/include/std/utility index ad30ad7..627f79b 100644 --- a/libstdc++-v3/include/std/utility +++ b/libstdc++-v3/include/std/utility @@ -194,6 +194,67 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } #endif + // Stores a tuple of indices. Used by tuple and pair, and by bind() to + // extract the elements in a tuple. + template + struct _Index_tuple + { + typedef _Index_tuple<_Indexes..., sizeof...(_Indexes)> __next; + }; + + // Builds an _Index_tuple<0, 1, 2, ..., _Num-1>. + template + struct _Build_index_tuple + { + typedef typename _Build_index_tuple<_Num - 1>::__type::__next __type; + }; + + template<> + struct _Build_index_tuple<0> + { + typedef _Index_tuple<> __type; + }; + +#if __cplusplus > 201103L + /// Class template integer_sequence + template + struct integer_sequence + { + typedef _Tp value_type; + static constexpr size_t size() { return sizeof...(_Idx); } + }; + + template::__type> + struct _Make_integer_sequence; + + template + struct _Make_integer_sequence<_Tp, _Num, _Index_tuple<_Idx...>> + { + static_assert( _Num >= 0, + "Cannot make integer sequence of negative length" ); + + typedef integer_sequence<_Tp, static_cast<_Tp>(_Idx)...> __type; + }; + + /// Alias template make_integer_sequence + template + using make_integer_sequence + = typename _Make_integer_sequence<_Tp, _Num>::__type; + + /// Alias template index_sequence + template + using index_sequence = integer_sequence; + + /// Alias template make_index_sequence + template + using make_index_sequence = make_integer_sequence; + + /// Alias template index_sequence_for + template + using index_sequence_for = make_index_sequence; +#endif + _GLIBCXX_END_NAMESPACE_VERSION } // namespace diff --git a/libstdc++-v3/testsuite/20_util/integer_sequence/intseq.cc b/libstdc++-v3/testsuite/20_util/integer_sequence/intseq.cc new file mode 100644 index 0000000..0f3ed41 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/integer_sequence/intseq.cc @@ -0,0 +1,27 @@ +// { dg-options "-std=gnu++1y" } +// { dg-do compile } +// +// Copyright (C) 2013 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 +#include + +using std::integer_sequence; + +static_assert( integer_sequence::size() == 0, "size() == 0" ); +static_assert( integer_sequence::size() == 3, "size() == 3" ); diff --git a/libstdc++-v3/testsuite/20_util/integer_sequence/requirements/typedefs.cc b/libstdc++-v3/testsuite/20_util/integer_sequence/requirements/typedefs.cc new file mode 100644 index 0000000..ab26800 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/integer_sequence/requirements/typedefs.cc @@ -0,0 +1,62 @@ +// { dg-options "-std=gnu++1y" } +// { dg-do compile } +// +// Copyright (C) 2013 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 +#include + +using std::is_same; +using std::integer_sequence; +using std::make_integer_sequence; +using std::index_sequence; +using std::make_index_sequence; +using std::index_sequence_for; + +static_assert( is_same::value_type, int>::value, + "int value_type"); + +static_assert( is_same::value_type, short>::value, + "short value_type"); + +static_assert( is_same, + integer_sequence>::value, + "make empty int seq" ); + +static_assert( is_same, + integer_sequence>::value, + "make non-empty int seq" ); + +static_assert( is_same, + integer_sequence>::value, + "make empty unsigned seq" ); + +static_assert( is_same, + integer_sequence>::value, + "make non-empty unsigned seq" ); + +static_assert( is_same, + integer_sequence>::value, + "index seq" ); + +static_assert( is_same, index_sequence<0, 1>>::value, + "make index seq" ); + +static_assert( is_same, + index_sequence<0, 1, 2, 3>>::value, + "index_sequence_for" );