Liste des Groupes | Revenir à cl c |
On 21/08/2024 12:17, Thiago Adams wrote:Compile time. f is not visible outside.On 21/08/2024 01:42, Blue-Maned_Hawk wrote:Run-time or compile-time error?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
It won't be a compile-time error if there is another 'f' visible in that outer scope.For this type of error you need my static analyzer. (cake) :D
And it won't be one here either:
if (FILE* f = fopen("file.txt", "r"))
{
/*...*/
fclose(f);
fwrite(f, ..) ;// ERROR
}
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:It is something optional to use.
FILE* f;
f = fopen("file.txt", "r");
if (f) {
// ...
fclose(f);
}
However I'd be more likely check for failure first.
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.
Les messages affichés proviennent d'usenet.