Deutsch English Français Italiano |
<va4oeo$3s6p8$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!2.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Thiago Adams <thiago.adams@gmail.com> Newsgroups: comp.lang.c Subject: Re: on allowing "int a" definition everywhere Date: Wed, 21 Aug 2024 10:01:12 -0300 Organization: A noiseless patient Spider Lines: 76 Message-ID: <va4oeo$3s6p8$1@dont-email.me> References: <afdfe7c37c6ad739fd82c7ec0587b82e0963fce2@i2pn2.org> <va2i90$3f4dg$1@dont-email.me> <pan$8a32c$1fb86219$8ea0c6ae$7c2d1765@invalid.invalid> <va4id0$3rc3n$1@dont-email.me> <va4mtb$3rvat$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Wed, 21 Aug 2024 15:01:13 +0200 (CEST) Injection-Info: dont-email.me; posting-host="87ebdc8a2a448433a767a2e33ec314f2"; logging-data="4070184"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+SNxBrpok5RVjKwiiGaonu+JDxqQKv66c=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:grCs/0R4PYzdhcjQyx9/2GIF544= In-Reply-To: <va4mtb$3rvat$1@dont-email.me> Content-Language: en-US Bytes: 3469 On 21/08/2024 09:34, Bart wrote: > On 21/08/2024 12:17, Thiago Adams wrote: >> On 21/08/2024 01:42, Blue-Maned_Hawk wrote: >>> Thiago Adams wrote: >>> >>>> initializer inside if is already in C++, and it will probably be on >>>> C2Y. >>> If it's for consistency with how for loops permit declarations, i would >>> _much_ prefer that they just outlaw that to induce consistency. >>> >>> (Really, i'd ideally want things to just stay as they are, since >>> declarations in for loops are simply too useful for macros.) >> >> I like the ability to declare things inside if. >> >> if (FILE* f = fopen("file.txt", "r")) >> { >> /*...*/ >> fclose(f); >> } >> >> Because it makes the scope of f, associated with the pointed object >> lifetime. >> >> For instance, if you try to use f >> >> if (FILE* f = fopen("file.txt", "r")) >> { >> /*...*/ >> fclose(f); >> } >> fwrite(f, ..) ;// ERROR > > Run-time or compile-time error? Compile time. f is not visible outside. > > It won't be a compile-time error if there is another 'f' visible in that > outer scope. > > And it won't be one here either: > > if (FILE* f = fopen("file.txt", "r")) > { > /*...*/ > fclose(f); > fwrite(f, ..) ;// ERROR > } > For this type of error you need my static analyzer. (cake) :D http://thradams.com/cake/playground.html?code=I3ByYWdtYSBzYWZldHkgZW5hYmxlCiNpbmNsdWRlIDxzdGRpby5oPgppbnQgbWFpbigpCnsKICAgRklMRSogX093bmVyIF9PcHQgZjsKICAgaWYgKCBmID0gZm9wZW4oImZpbGUudHh0IiwgInIiKSkKICAgewogICAgY2hhciBhW109ImFiIjsKICAgIGZjbG9zZShmKTsgICAgIAogICAgZndyaXRlKGEsIDEsIDIsIGYpOyAgICAgCiAgIH0KfQo%3D&to=-1&options= > Meanwhile, I'd have a serious problem with the extra clutter this > causes. Since you are not reporting an fopen failure, I'd write this as: > > FILE* f; > > f = fopen("file.txt", "r"); > if (f) { > // ... > fclose(f); > } > > However I'd be more likely check for failure first. It is something optional to use. > Since I did a recent survery where, on a average, functions only had 3 > local variables (in a selection of apps), I've been even more sceptical > of block-scoped variables. > >