Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c
Subject: Re: Comma operator in for() statements
Date: Fri, 27 Mar 2020 06:46:06 -0700
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <864ku9ap29.fsf@linuxsc.com>
References: <4fbkkg-2kr1.ln1@mail.home.palmen-it.de> <4fa819d5-bc63-4c81-bc3f-b328dfc0008a@googlegroups.com> <1a9cdf79-5489-43d0-a050-888012804ca2@googlegroups.com> <87zhc72f0u.fsf@bsb.me.uk> <6ed3fe7f-aa6e-41b7-9753-285b616c2cf4@googlegroups.com> <87eetirfyb.fsf@nosuchdomain.example.com> <871rpirdt3.fsf@nosuchdomain.example.com> <5788dcca-ca28-4fb4-b454-89436c601d0c@googlegroups.com> <87wo7aps7r.fsf@nosuchdomain.example.com> <87y2rq0zmy.fsf@bsb.me.uk> <27ca29d7-392f-443a-be90-097ed390a674@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="02cbe0882c270f81c5b8b4aa13e82222"; logging-data="10934"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/+vbawPoVV7uvzVVeDc+c1XKEsFvU2w+k="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:6NQMexzmbQClm9Qazi3VjF9yvgg= sha1:p08mCA8UK1JTA7tC6j2gO7pHRy0=
Xref: csiph.com comp.lang.c:150856
luser droog writes:
> [how to make for() variables be const]
>
> I've tried to shake the habit of seeing macros everywhere,
> but this is getting to the length where a macro might be an
> appropriate way to use this idiom.
>
> #define FOR_UP(IDX, INI, LIM, BODY) \
> for( int IDX ## __LINE__ = (INI); \
> IDX ## __LINE__ < (LIM); \
> ++ IDX ## __LINE__ ){ \
> int const IDX = IDX ## __LINE__; \
> BODY; \
> }
Maybe so but this macro isn't it.
(1) Type of iteration variable should be a parameter,
not fixed.
(2) The usage '## __LINE__' doesn't work.
(3) The "test" and "increment" should both be more
general.
(4) Perhaps most important, the loop body should
not be a parameter. Ick and double ick.
Challenge: write a macro definition that addresses each
of the above concerns. Specifically with regard to point
(4), the macro expansion should act like a 'for(...)'
prefix, so the loop body statement comes after the macro
call, eg,
P_FOR( int, i, 0, < 10, ++ ){
sum += a[i];
}
(Yes there are hints there for some of the other points.)