Sujet : Re: "A diagram of C23 basic types"
De : bc (at) *nospam* freeuk.com (bart)
Groupes : comp.lang.cDate : 04. Apr 2025, 15:14:45
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vsopgk$3g9f5$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10
User-Agent : Mozilla Thunderbird
On 04/04/2025 14:38, David Brown wrote:
On 04/04/2025 04:50, Janis Papanagnou wrote:
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).
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!).
There are appear to be two new features:
* Allowing a declaration where a conditional expresson normally goes
* Having several things there separated with ";" (yes, here ";" is a separator, not a terminator).
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.
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'!
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.)
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.)