Re: on allowing "int a" definition everywhere

Liste des GroupesRevenir à cl c  
Sujet : Re: on allowing "int a" definition everywhere
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.c
Date : 22. Aug 2024, 11:21:56
Autres entêtes
Organisation : None to speak of
Message-ID : <87h6bchpzf.fsf@nosuchdomain.example.com>
References : 1 2 3 4 5
User-Agent : Gnus/5.13 (Gnus v5.13)
Blue-Maned_Hawk <bluemanedhawk@invalid.invalid> writes:
Thiago Adams wrote:
[...]
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
>
You can already do that in C23:

Are you suggesting that there's some relevant new feature in C23?
I don't believe there is.

if (…) {
FILE * f = fopen("file.txt", "r");
/* … */
fclose(f);
}
fwrite(f, …);  /* Some kind of error happens. */

I presume the "…" is meant to stand in for some arbitary code, not that
the ellipsis is part of the C23 syntax.

That's not at all the same thing.  In Thiago's code, the body
of the if statement is not executed if the fopen() call fails.
Note that fclose(f) has undefined behavior of f==NULL.

Or, if you need it to exist before the controlling expression:
>
for (bool x = true; x; x = false)  for (FILE * f = fopen("file.txt", "r");
x; x = false)  if (…) {
/* … */
fclose(f);
}
fwrite(f, …);
>
Therefore, your example does not work as evidence in favor of declarations
in if statement controlling expressions because it's already possible in
other ways.

I'm not going to wade through that, but *of course* you can write code
that does what Thiago's code is intended to do.  The point of allowing
declarations in if conditions is for convenience, the same reason
they're allowed in for loops starting in C99.

The original code:

    if (FILE* f = fopen("file.txt", "r"))
    {
       /*...*/ fclose(f);
    }

doesn't allow for taking some non-trivial action of the fopen() call
fails, but if a declaration in an if condition is visible in the else
clause, you could write something like:

    if (FILE* f = fopen("file.txt", "r")) {
       /*...*/
       fclose(f);
    }
    else {
        perror("file.txt");
        exit(EXIT_FAILURE); // or try something else
    }

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */

Date Sujet#  Auteur
20 Aug 24 * on allowing "int a" definition everywhere62fir
20 Aug 24 +* Re: on allowing "int a" definition everywhere24Thiago Adams
20 Aug 24 i+* Re: on allowing "int a" definition everywhere4fir
20 Aug 24 ii+- Re: on allowing "int a" definition everywhere1fir
20 Aug 24 ii`* Re: on allowing "int a" definition everywhere2Thiago Adams
20 Aug 24 ii `- Re: on allowing "int a" definition everywhere1fir
21 Aug 24 i`* Re: on allowing "int a" definition everywhere19Blue-Maned_Hawk
21 Aug 24 i `* Re: on allowing "int a" definition everywhere18Thiago Adams
21 Aug 24 i  +* Re: on allowing "int a" definition everywhere4Bart
21 Aug 24 i  i`* Re: on allowing "int a" definition everywhere3Thiago Adams
21 Aug 24 i  i `* Re: on allowing "int a" definition everywhere2Michael S
21 Aug 24 i  i  `- Re: on allowing "int a" definition everywhere1Thiago Adams
22 Aug 24 i  `* Re: on allowing "int a" definition everywhere13Blue-Maned_Hawk
22 Aug 24 i   +* Re: on allowing "int a" definition everywhere5Keith Thompson
22 Aug 24 i   i`* Re: on allowing "int a" definition everywhere4Ben Bacarisse
22 Aug 24 i   i +- Re: on allowing "int a" definition everywhere1Keith Thompson
22 Aug 24 i   i `* Re: on allowing "int a" definition everywhere2Kaz Kylheku
23 Aug 24 i   i  `- Re: on allowing "int a" definition everywhere1Ben Bacarisse
22 Aug 24 i   `* Re: on allowing "int a" definition everywhere7Bart
22 Aug 24 i    +* Re: on allowing "int a" definition everywhere2Thiago Adams
22 Aug 24 i    i`- Re: on allowing "int a" definition everywhere1Blue-Maned_Hawk
23 Aug 24 i    `* Re: on allowing "int a" definition everywhere4fir
23 Aug 24 i     `* Re: on allowing "int a" definition everywhere3Bart
23 Aug 24 i      `* Re: on allowing "int a" definition everywhere2fir
23 Aug 24 i       `- Re: on allowing "int a" definition everywhere1fir
21 Aug 24 `* Re: on allowing "int a" definition everywhere37Lawrence D'Oliveiro
21 Aug 24  +* Re: on allowing "int a" definition everywhere2Ben Bacarisse
21 Aug 24  i`- Re: on allowing "int a" definition everywhere1Ben Bacarisse
25 Aug 24  `* Re: on allowing "int a" definition everywhere34fir
27 Aug 24   `* Re: on allowing "int a" definition everywhere33Lawrence D'Oliveiro
27 Aug 24    +* Re: on allowing "int a" definition everywhere21fir
27 Aug 24    i`* Re: on allowing "int a" definition everywhere20fir
27 Aug 24    i `* Re: on allowing "int a" definition everywhere19fir
27 Aug 24    i  +* Re: on allowing "int a" definition everywhere3fir
27 Aug 24    i  i`* Re: on allowing "int a" definition everywhere2fir
27 Aug 24    i  i `- Re: on allowing "int a" definition everywhere1fir
27 Aug 24    i  `* Re: on allowing "int a" definition everywhere15fir
27 Aug 24    i   +* Re: on allowing "int a" definition everywhere13fir
27 Aug 24    i   i+* Re: on allowing "int a" definition everywhere9Bart
27 Aug 24    i   ii+* Re: on allowing "int a" definition everywhere3fir
27 Aug 24    i   iii`* Re: on allowing "int a" definition everywhere2fir
27 Aug 24    i   iii `- Re: on allowing "int a" definition everywhere1fir
2 Sep 24    i   ii`* Re: on allowing "int a" definition everywhere5Lawrence D'Oliveiro
2 Sep 24    i   ii `* Re: on allowing "int a" definition everywhere4Bart
3 Sep 24    i   ii  `* Re: on allowing "int a" definition everywhere3Lawrence D'Oliveiro
3 Sep 24    i   ii   +- Re: on allowing "int a" definition everywhere1Kaz Kylheku
3 Sep 24    i   ii   `- Re: on allowing "int a" definition everywhere1Michael S
27 Aug 24    i   i`* Re: on allowing "int a" definition everywhere3fir
29 Aug 24    i   i `* Re: on allowing "int a" definition everywhere2fir
29 Aug 24    i   i  `- Re: on allowing "int a" definition everywhere1fir
3 Sep 24    i   `- Re: on allowing "int a" definition everywhere1Lawrence D'Oliveiro
27 Aug 24    `* Re: on allowing "int a" definition everywhere11Blue-Maned_Hawk
28 Aug 24     `* Re: on allowing "int a" definition everywhere10Tim Rentsch
28 Aug 24      `* Re: on allowing "int a" definition everywhere9Keith Thompson
28 Aug 24       +* Re: on allowing "int a" definition everywhere2Ben Bacarisse
28 Aug 24       i`- Re: on allowing "int a" definition everywhere1Tim Rentsch
28 Aug 24       +* Re: on allowing "int a" definition everywhere2Tim Rentsch
28 Aug 24       i`- Re: on allowing "int a" definition everywhere1Keith Thompson
28 Aug 24       `* Re: on allowing "int a" definition everywhere4Tim Rentsch
28 Aug 24        +- Re: on allowing "int a" definition everywhere1David Brown
28 Aug 24        +- Re: on allowing "int a" definition everywhere1James Kuyper
28 Aug 24        `- Re: on allowing "int a" definition everywhere1Keith Thompson

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal