Sujet : Re: Code guidelines
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.lang.cDate : 04. Sep 2024, 19:20:55
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vba8e8$3u4jc$3@dont-email.me>
References : 1 2 3 4 5 6 7
User-Agent : Mozilla Thunderbird
On 04/09/2024 16:02, Kaz Kylheku wrote:
On 2024-09-03, Thiago Adams <thiago.adams@gmail.com> wrote:
The contract is
>
* obj->member1 CAN be null
* obj->member1->member2 CANNOT be null
* obj->member1->member2->member3 CAN be null
Newer languages have null-safe object access operators:
if (obj?->member1->member2?->member3) ...
We get the correct check, and, at a glance, the question marks annotate
what the coder believes is the contract: obj may be null,
obj->member1->member2 may be null.
GCC could easily get this extension, it seems.
obj?->member is just obj ? obj->member : nullptr except that
obj is evaluated only once.
gcc is not as keen on making new extension syntax as it was some 30 year ago. They try to keep extensions to a minimum, or fitting them into existing patterns (__builtin functions and __attribute__'s). Or they import new features from later C or C++ standards back to older standard versions.
And they tend to have the attitude that programmers who want a language that does more than current C, will probably use a different language. In C++ you can often specify non-null requirements using references rather than pointers, and it's not hard to make a little class template that act mostly like a pointer except that it checks for null first and throws an exception on null. (Making something that will chain like your code, resulting in a bool true if everything is good to go, is I believe possible but a bit more fiddly. Ask for details down the hall.)