Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

return a ?? b;

83 views
Skip to first unread message

fir

unread,
Oct 25, 2022, 2:08:46 PM10/25/22
to
seen a meme like that
what does it do? can it be often useful?

David Brown

unread,
Oct 25, 2022, 3:53:00 PM10/25/22
to
On 25/10/2022 20:08, fir wrote:
> seen a meme like that
> what does it do? can it be often useful?

There is no "??" operator in C. But it exists in C#, and perhaps other
languages. In C# it is the "null coalescing operator". "a ?? b" will
evaluate to "a" if "a" is not null, otherwise "b" is evaluated and
returned. Basically :

return a ?? b;

means the same as :

return (a != NULL) ? a : b;

in C.


Kenny McCormack

unread,
Oct 25, 2022, 4:18:35 PM10/25/22
to
In article <tj9eql$25qrn$1...@dont-email.me>,
Gnu C has ?:, where:

a ?: b

returns a if a is non-zero, else b.

--
You are again heaping damnation upon your own head by your statements.

- Rick C Hodgin -

David Brown

unread,
Oct 26, 2022, 4:11:14 AM10/26/22
to
On 25/10/2022 22:18, Kenny McCormack wrote:
> In article <tj9eql$25qrn$1...@dont-email.me>,
> David Brown <david...@hesbynett.no> wrote:
>> On 25/10/2022 20:08, fir wrote:
>>> seen a meme like that
>>> what does it do? can it be often useful?
>>
>> There is no "??" operator in C. But it exists in C#, and perhaps other
>> languages. In C# it is the "null coalescing operator". "a ?? b" will
>> evaluate to "a" if "a" is not null, otherwise "b" is evaluated and
>> returned. Basically :
>>
>> return a ?? b;
>>
>> means the same as :
>>
>> return (a != NULL) ? a : b;
>>
>> in C.
>>
>>
>
> Gnu C has ?:, where:
>
> a ?: b
>
> returns a if a is non-zero, else b.
>

So it does - and that avoids evaluating "a" twice. (I suppose that is
the point of the extension.) I've never seen it in use, and I'd be wary
of using it myself. gcc is much less keen on such extensions than they
used to be, and it could go the way of their "min" and "max" operators
("?<" and "?>", IIRC).


fir

unread,
Oct 26, 2022, 3:07:03 PM10/26/22
to
ok ynx i see it on meme ant there was no language stated though it fully looked like C

wonder if it would git to c, though my comprehension levels dropped,
it seems hovever that it is possibly not bad idea

Kenny McCormack

unread,
Oct 27, 2022, 5:10:15 AM10/27/22
to
In article <tjaq2g$2e6ub$1...@dont-email.me>,
David Brown <david...@hesbynett.no> wrote:
...
>gcc is much less keen on such extensions than they
>used to be, and it could go the way of their "min" and "max" operators
>("?<" and "?>", IIRC).

Two comments:

1) According to:

https://stackoverflow.com/questions/3437410/c-extension-and-operators

it is <? and >?
and
2) It is (was) only in g++, not gcc.
This is significant, because in C++, you can always use std::min/max
(or something like that - I don't do C++). Which is to say this would
have been a useful extension to keep in straight C, but is (was)
superfluous in C++.

Anyway, that site seems to agree with you - that you "should" not use these
extensions, else the boogie men will eat you for lunch.

--
The randomly chosen signature file that would have appeared here is more than 4
lines long. As such, it violates one or more Usenet RFCs. In order to remain
in compliance with said RFCs, the actual sig can be found at the following URL:
http://user.xmission.com/~gazelle/Sigs/ForFoxViewers

David Brown

unread,
Oct 27, 2022, 5:37:29 AM10/27/22
to
On 27/10/2022 11:10, Kenny McCormack wrote:
> In article <tjaq2g$2e6ub$1...@dont-email.me>,
> David Brown <david...@hesbynett.no> wrote:
> ...
>> gcc is much less keen on such extensions than they
>> used to be, and it could go the way of their "min" and "max" operators
>> ("?<" and "?>", IIRC).
>
> Two comments:
>
> 1) According to:
>
> https://stackoverflow.com/questions/3437410/c-extension-and-operators
>
> it is <? and >?

That shows how often I have seen them in the wild!

> and
> 2) It is (was) only in g++, not gcc.

I didn't remember that detail.

> This is significant, because in C++, you can always use std::min/max
> (or something like that - I don't do C++). Which is to say this would
> have been a useful extension to keep in straight C, but is (was)
> superfluous in C++.

gcc has other extensions that are much more generally useful, and which
let you make "ideal" min/max macros that work with any type and avoid
the multiple evaluation issue of traditional (standards-compliant) C
min/max macros :

<https://gcc.gnu.org/onlinedocs/gcc/Typeof.html>
<https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html>

#define max(a,b) \
({ typeof (a) _a = (a); \
typeof (b) _b = (b); \
_a > _b ? _a : _b; })


(In C++ you have templates and don't need these gcc C extensions.)

>
> Anyway, that site seems to agree with you - that you "should" not use these
> extensions, else the boogie men will eat you for lunch.
>

If someone on Stack Overflow agrees with me, I /must/ be right :-)

fir

unread,
Oct 27, 2022, 7:24:12 AM10/27/22
to
btw as i already said i am writing my own extended/future c compiler named furia
and think hopw to do things there (its not quite c on topic but partially it is becouse
it shares same groud with c on which it was born, and also many other things
though syntaxical skin of it is different)

(by writing i mean form time to time and if i write i post on it here so as its seen
i dont touched it for months... now i could return ad writa a bit but some ral life
problems fire my poor brain nd body so i cant do that)

hovever i could try to think how make all that if syntax here, (and thsi is related to this ?
sign)

as i see it now the if dont need a syntax i mean my use space but needs
only a form like
a<b or f<g w t y z

when the thing on left "a<b or f<g" (foirgot how it is called (condition?) ) is
dounted and then statemnts on right "w t y z" are executed so it seems i only need to
work out a form for this leftside condition expressions (dont need if keyword nor "?")
(hovver i could maybe use ? optionally
a<b or f<g ? w t y z
and it seems i need else sign

right now (last time) i used

last_key_pressed?=keyB
like in

last_key_pressed?=keyB {foo 2|3|4|5,foo 6 7 8 9}

whis i now dont like , even possibly old == would be better

last_key_pressed==keyB {foo 2|3|4|5,foo 6 7 8 9}

though its also not optimal, but possibly better



fir

unread,
Oct 27, 2022, 8:12:39 AM10/27/22
to
side question is it possible to make such various syntaxes of conditions like this below
shorter?

a<4 and b==3 or c>-a

note i resign from using & and | becouse it looks bad keywords and and or seem better to me (or at least could use something but not & and \ symbols but something more readable/good loking)
i mean i need a syntax set/rules to write such logical expressions best way

as i once said probably such thing
0<=x<640
would be nice idea i mean it checks if x>=0 and x<640 at once (its shoprter then)


fir

unread,
Oct 27, 2022, 9:25:43 AM10/27/22
to
and is done naturally by doing nothing like

a==1 b==2 c==4 foo
is
a==1 {b==2 {c==4 {foo}}}
but i dont know if it is enough to skipp all ands

a==1 and b==2 or d==3 and c==4 or g==8 foo

a==1 b==2 or d==3 c==4 or g==8 foo

is it good? is this enough readable?

Kaz Kylheku

unread,
Oct 27, 2022, 11:01:52 AM10/27/22
to
On 2022-10-27, David Brown <david...@hesbynett.no> wrote:
><https://gcc.gnu.org/onlinedocs/gcc/Typeof.html>
><https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html>
>
> #define max(a,b) \
> ({ typeof (a) _a = (a); \
> typeof (b) _b = (b); \
> _a > _b ? _a : _b; })
>

We can implement a ?: b also:

#define or(a, b) \
({ typeof (a) or_t_m_p = (a); \
or_t_m_p ? or_t_m_p : (b); })

> (In C++ you have templates and don't need these gcc C extensions.)

But could a template implement the evaluation semantics of or(x, y) above?

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal

David Brown

unread,
Oct 27, 2022, 1:40:35 PM10/27/22
to
On 27/10/2022 17:01, Kaz Kylheku wrote:
> On 2022-10-27, David Brown <david...@hesbynett.no> wrote:
>> <https://gcc.gnu.org/onlinedocs/gcc/Typeof.html>
>> <https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html>
>>
>> #define max(a,b) \
>> ({ typeof (a) _a = (a); \
>> typeof (b) _b = (b); \
>> _a > _b ? _a : _b; })
>>
>
> We can implement a ?: b also:
>
> #define or(a, b) \
> ({ typeof (a) or_t_m_p = (a); \
> or_t_m_p ? or_t_m_p : (b); })
>
>> (In C++ you have templates and don't need these gcc C extensions.)
>
> But could a template implement the evaluation semantics of or(x, y) above?
>

No, you can't control evaluation semantics in a template function in C++
- it's a function, so all arguments are evaluated exactly once (in an
indeterminate order) before the template function itself is evaluated.

So you still need macros, but you don't need extensions:

#define Or(a, b) []{auto ta = a; return ta ? ta : b; }()

("or" is a keyword in C++ - "a or b" is the same as "a || b".)


However, I really don't think you'd want the semantics of the "or" macro
in a function-like macro. It would be confusing to users - it looks
like a function, and people would expect evaluation to work the same
way. The problem with a traditional simple "max" macro is that one of
the arguments is going to be evaluated twice, and that is unexpected.



fir

unread,
Oct 28, 2022, 6:38:59 AM10/28/22
to
czwartek, 27 października 2022 o 15:25:43 UTC+2 fir napisał(a):
treating and as a kinda passthru (?) is imo much faster readable than and
(at least for me), like

a==1 -> b==2 or d==3 - > c==4 or g==8 -> foo
or
a==1: b==2 or d==3: c==4 or g==8 : foo

imo more readablke than and
thats why i often prefer to write
if(a) if(b>10) if(c) {}
than using ands

hovever if and is removed here then there is thought if also remove 'or' maybe

a==1: b==2 or d==3: c==4 or g==8 : foo


or replacy by some opartor again '|' i cannot use as | is ideal for making vectors 1|2|3 etc
0 new messages