Return-Path: Delivered-To: apmail-stdcxx-dev-archive@www.apache.org Received: (qmail 56666 invoked from network); 14 Jul 2008 20:54:59 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 14 Jul 2008 20:54:59 -0000 Received: (qmail 91524 invoked by uid 500); 14 Jul 2008 20:54:53 -0000 Delivered-To: apmail-stdcxx-dev-archive@stdcxx.apache.org Received: (qmail 91511 invoked by uid 500); 14 Jul 2008 20:54:53 -0000 Mailing-List: contact dev-help@stdcxx.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@stdcxx.apache.org Delivered-To: mailing list dev@stdcxx.apache.org Received: (qmail 91416 invoked by uid 99); 14 Jul 2008 20:54:52 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 14 Jul 2008 13:54:52 -0700 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: local policy) Received: from [208.30.140.160] (HELO moroha.roguewave.com) (208.30.140.160) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 14 Jul 2008 20:53:58 +0000 Received: from exchmail01.Blue.Roguewave.Com (exchmail01.blue.roguewave.com [10.22.129.22]) by moroha.roguewave.com (8.13.6/8.13.6) with ESMTP id m6EKqJ4q017480 for ; Mon, 14 Jul 2008 20:52:20 GMT X-MimeOLE: Produced By Microsoft Exchange V6.5 Content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Subject: RE: Forward iteration in variadic templates Date: Mon, 14 Jul 2008 14:52:15 -0600 Message-ID: X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: Forward iteration in variadic templates Thread-Index: Acjl7+wxkado5QMxQtOqwZiIg2qgtQAAEI0gAAB1eiA= References: From: "Travis Vitek" To: X-Virus-Checked: Checked by ClamAV on apache.org =20 Eric Lemings wrote: > >> Eric Lemings wrote: >>=20 >> Consider the following function: >>=20 >> template >> void print (const Types&... values); >>=20 >> How would you define print() to iterate forward from 0..N,=20 >> where N is sizeof... (Types), printing each argument X I'm unsure about what you mean by iterate here. AFAIK, you can't iterate over the elements in a parameter pack directly, everything is done with recursion that terminates with one or more special cases. > >...and its corresponding index... > >> in the=20 >> argument list (e.g. std::cout << X)? >>=20 >> Thanks, >> Brad. >>=20 > I don't know if this is what you had in mind, but it does what you ask. $ cat q.cpp && g++ -std=3Dgnu++0x q.cpp && ./a.out #include template void print_impl (unsigned index, const _TTypes&... values); template void print_impl (unsigned index, const _TypeT& value) { std::cout << index << ':' << value << '\n'; } template void print_impl (unsigned index, const _TypeT& value, const _TTypes&... values) { print_impl (index, value); print_impl (index + 1, values...); } template void print (const _TTypes&... values) { print_impl (0, values...); } int main () { print (1, 'a', 3.14f, "hello"); return 0; } 0:1 1:a 2:3.14 3:hello