Sujet : Re: size_t best practice
De : tr.17687 (at) *nospam* z991.linuxsc.com (Tim Rentsch)
Groupes : comp.lang.cDate : 22. Aug 2024, 09:38:24
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <86a5h59clb.fsf@linuxsc.com>
References : 1 2 3 4
User-Agent : Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Andrey Tarasevich <
andreytarasevich@hotmail.com> writes:
On 08/20/24 6:55 AM, Andrey Tarasevich wrote:
>
On 08/20/24 6:53 AM, Andrey Tarasevich wrote:
>
For example, iteration all the way to 0 can be idiomatically
implemented as
>
for (some_unsigned_type i = size; (some_unsigned_type) i != -1; --i)
...
>
Sorry, a typo. Was meant to be
>
for (some_unsigned_type i = size; i != (some_unsigned_type) -1; --i)
...
>
Of, crap. One more time
>
for (some_unsigned_type i = size - 1;
i != (some_unsigned_type) -1;
--i)
...
>
Should be good now :)
To me the redundant cast is a red flag. A cleaner alternative:
for( some_unsigned_type i = size; i > 0 && i--; ){
...
}
Produces identical code to the above at -O1 (both gcc and clang).