Liste des Groupes | Revenir à cl c |
On 04/04/2025 14:38, David Brown wrote:Yes, that is correct. The feature has made it into the public drafts for the post-C23 version of C standards, but I have no idea when that will be complete.On 04/04/2025 04:50, Janis Papanagnou wrote:So, this is a proposal still for C, as it doesn't work for any current version of C (I should have read the above more carefully first!).Really, that recent!? - I was positive that I used it long before 2017>
during the days when I did quite regularly C++ programming. - Could it
be that some GNU compiler (C++ or "C") supported that before it became
C++ standard?
>
Janis
>
To be clear, we are talking about :
>
if (int x = get_next_value(); x > 10) {
// We got a big value!
}
>
It was added in C++17. <https://en.cppreference.com/w/cpp/language/if>
>
gcc did not have it as an extension, but they might have had it in the pre-standardised support for C++17 (before C++17 was published, gcc had "-std=c++1z" to get as many proposed C++17 features as possible before they were standardised. gcc has similar "pre-standard" support for all C and C++ versions).
There are appear to be two new features:Yes.
* Allowing a declaration where a conditional expresson normally goes
* Having several things there separated with ";" (yes, here ";" is a separator, not a terminator).Two things, rather than several.
Someone said they weren't excited by my proposal of being able to leave out '#include <stdio.>'. Well I'm not that excited by this.OK. I would not expect you to be - you prefer your variables to be declared at the head of functions rather than at minimal scope. For other C programmers, this is similar to "for (int i = 0; i < 10; i++)" and will likely be quite popular once C23 support gets established.
In fact I would actively avoid such a feature, as it adds clutter to code. It might look at first as though it saves you having to add a separate declaration, until you're writing the pattern for the fourth time in your function and realised you now have 4 declarations for 'x'!I think your counting is off.
And also the type of 'x' is hardcoded in four places instead of one (so if 'get_next_value' changes its return type, you now have more maintenance and a real risk of missing out one).
(If you say that those 4 instances could call different functions so each 'x' is a different type, then it would be a different kind of anti-pattern.)Yes, small scope is the point. Small scopes are better than big scopes (within reason).
Currently it would need this (it is assumed that 'x' is needed in the body):
int x;
if ((x = getnextvalue()) > 10) {
// We got a big value!
}
It's a little cleaner. (Getting rid of those outer parameter parentheses would be far more useful IMO.)
(My language can already do this stuff:
if int x := get_next_value(); x > 10 then
println "big"
fi
But it is uncommon, and it would be over-cluttery even here. However I don't have the local scope of 'x' that presumably is the big deal in the C++ feature.)
Les messages affichés proviennent d'usenet.