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

Trying out _Generic...

112 views
Skip to first unread message

Chris M. Thomasson

unread,
Mar 31, 2018, 7:19:24 PM3/31/18
to
So here is what I quickly came up with, keep in mind that I never really
used _Generic before except with tgmath.h:
_________________________________
#include <stdio.h>


struct foo_0
{
int blah0;
};

struct foo_1
{
int blah0;
int blah1;
};


void foo_0(struct foo_0* self, int a, int b)
{
printf("foo_0(%p, blah0: %u, %u, %u)\n",
(void*)self, self->blah0, a, b);
}

void foo_1(struct foo_1* self, int a, int b)
{
printf("foo_1(%p, blah0: %u, blah1: %u, %u, %u)\n",
(void*)self, self->blah0, self->blah1, a, b);
}


#define foo(a, ...) \
(_Generic((a), \
struct foo_0*: foo_0, \
struct foo_1*: foo_1 \
)((a), __VA_ARGS__))


int main(void)
{
struct foo_0 f0 = { 1 };
struct foo_1 f1 = { 2, 3 };

foo(&f0, 4, 5); // Calls foo_0
foo(&f1, 4, 5); // Calls foo_1

return 0;
}
_________________________________


As you can see, this code allows me to call the right implementation of
foo, as foo_0 or foo_1 depending on the type of the first parameter.
Now, can you see anything terribly wrong here? Is this Kosher?

Thanks.

Chris M. Thomasson

unread,
Mar 31, 2018, 7:23:20 PM3/31/18
to
On 3/31/2018 4:19 PM, Chris M. Thomasson wrote:
> So here is what I quickly came up with, keep in mind that I never really
> used _Generic before except with tgmath.h:
> _________________________________
> #include <stdio.h>
>
>
> struct foo_0
> {
>     int blah0;
> };
[...]
> void foo_0(struct foo_0* self, int a, int b)
> {
>     printf("foo_0(%p, blah0: %u, %u, %u)\n",
>            (void*)self, self->blah0, a, b);
[...]
> _________________________________
>
>
> As you can see, this code allows me to call the right implementation of
> foo, as foo_0 or foo_1 depending on the type of the first parameter.
> Now, can you see anything terribly wrong here? Is this Kosher?

Well, try to look past the fact that I am using the wrong type in
printf! %u instead of %d... Here, let me quickly fix this example code
by changing the types in foo_N structs to unsigned int:
_________________________________
#include <stdio.h>


struct foo_0
{
unsigned int blah0;
};

struct foo_1
{
unsigned int blah0;
unsigned int blah1;
};


void foo_0(struct foo_0* self, int a, int b)
{
printf("foo_0(%p, blah0: %u, %u, %u)\n",
(void*)self, self->blah0, a, b);
}

void foo_1(struct foo_1* self, int a, int b)
{
printf("foo_1(%p, blah0: %u, blah1: %u, %u, %u)\n",
(void*)self, self->blah0, self->blah1, a, b);
}


#define foo(a, ...) \
(_Generic((a), \
struct foo_0*: foo_0, \
struct foo_1*: foo_1 \
)((a), __VA_ARGS__))


int main(void)
{
struct foo_0 f0 = { 1 };
struct foo_1 f1 = { 2, 3 };

foo(&f0, 4, 5); // Calls foo_0
foo(&f1, 4, 5); // Calls foo_1

return 0;
}
_________________________________


Sorry about that non-sense. Well, that it what you get when the code is
quickly typed out.

;^o

Chris M. Thomasson

unread,
Apr 4, 2018, 12:16:55 AM4/4/18
to
On 3/31/2018 4:19 PM, Chris M. Thomasson wrote:
> So here is what I quickly came up with, keep in mind that I never really
> used _Generic before except with tgmath.h:
> _________________________________
[...]
> _________________________________
>
>
> As you can see, this code allows me to call the right implementation of
> foo, as foo_0 or foo_1 depending on the type of the first parameter.
> Now, can you see anything terribly wrong here? Is this Kosher?
>
> Thanks.

Can you anybody else get it to compile, and run?

Steven Petruzzellis

unread,
Apr 4, 2018, 12:23:43 AM4/4/18
to
I won't ask Vennie Bowden (https://www.facebook.com/DavidBowdenswife) how any part of The Flying Spaghetti Monster's body tastes no matter how often Vennie Bowden (https://www.facebook.com/DavidBowdenswife) licks it.

Real posters will be destroyed once the waters are overfished. That and there will invariably be a butthurt insomniac or two in an unmoderated news group (i.e. Vennie Bowden (https://www.facebook.com/DavidBowdenswife)). The current crap aside, we've all seen who regularly gets angry when he doesn't get a catch on his hook.

That's what Vennie Bowden (https://www.facebook.com/DavidBowdenswife) does when he gets caught. He creates a new identity, starts a thread so he can claim it was The Flying Spaghetti Monster. Hey it was my left hand... and then Vennie Bowden (https://www.facebook.com/DavidBowdenswife) talks to himself with his right hand. Right, Vennie Bowden (https://www.facebook.com/DavidBowdenswife) is looking to produce a DOM variable, which The Flying Spaghetti Monster can get in 5 seconds, that anyone can learn from a video. If he could stop being so slow he'd understand how screwed up he proves himself to be. Can UTF-8 characters be concatenated into a string with a logical connection to the "real" world; that is the question posed recently by those astonished at the endless babbling of "advocates" and basement-dwelling ultra-cranks.



--
This Trick Gets Women Hot For You
http://www.5z8.info/stalin-will-rise-again_j3h5uj_nakedgrandmas.jpg
https://www.reddit.com/r/linux/comments/6sfhq6/what_desktop_tasks_does_linux_handle_better_than/
https://goo.gl/Fho5Nq
Jonas Eklundh Communication AB

Melzzzzz

unread,
Apr 4, 2018, 12:36:34 AM4/4/18
to
~/News >>> gcc proggy.c
~/News >>> ./a.out
foo_0(0x7ffe7c17eb0c, blah0: 1, 4, 5)
foo_1(0x7ffe7c17eb10, blah0: 2, blah1: 3, 4, 5)



--
press any key to continue or any other to quit...

Steven Petruzzellis

unread,
Apr 4, 2018, 1:41:25 AM4/4/18
to
Thiago 'Super Troll' Adams's posts are nothing but an indecipherable mess.

Shawn Ulman has a LOT of insight to demonstrate and he apparently wants to answer questions. Unfortunately, this is arguably the worst locale for doing that because a large part of what he will get back is wailing, double-crossing, and other BS.

Thiago 'Super Troll' Adams doesn't know if there is a free open sores program to handle such tasks.

Proof Thiago 'Super Troll' Adams accuses everyone of being Snit http://sandman.net/files/snit_circus.png.

Recently I did work on and showed some C for the front end which is the only thing you can do when trying to avoid Thiago 'Super Troll' Adams's narcissistic crap while reading with Google Groups.

What were you thinking?

Thiago 'Super Troll' Adams asked to be rated according to objective trolling criteria, which Shawn Ulman obliged him with.

Can you stop asking for my attention?

--
This broke the Internet
http://www.5z8.info/trojan_p4g1zy_how-to-skin-a-gerbil
https://youtu.be/m37dF5X2S-4
http://www.5z8.info/open.exe_f2s2ea_nazi
Jonas Eklundh

Chris M. Thomasson

unread,
Apr 4, 2018, 3:53:27 PM4/4/18
to
Thank you Melzzzzz! The output is correct: Perfect.

Chris M. Thomasson

unread,
Apr 5, 2018, 3:43:34 PM4/5/18
to
I enjoy others being able to compile, and run my code. It creates a
calming feeling within myself. :^)

Steven Petruzzellis

unread,
Apr 6, 2018, 1:43:19 AM4/6/18
to
Don't look now, but I think Rip Off Artist Shawn Ulman has a serious fascination with Carlos E.R.. Can you get Carlos E.R. to agree? Unfortunately there are too many "bugs are acceptable" false "advocates" and not enough programers with the time to help the people with Linux gear.

No one who isn't just using you for trolling ends sees you as anything remotely close to good. You have nobody but Rip Off Artist Shawn Ulman to credit for that.

Having to tolerate the use of Systemd is not what most want to do. All that matters is that Rip Off Artist Shawn Ulman gets to convey his spam message and then hang up and giggle about it. The fact that Carlos E.R. is a real person on the other end of the phone is his motivation. I would respond to Rip Off Artist Shawn Ulman honestly but he is a cretin who plays trolling games to fulfill his appetite to call everyone a cheat.

--
This broke the Internet
http://www.5z8.info/hack-outlook_c6c9in_michaelangelo-virus
Jonas Eklundh

bartc

unread,
Apr 6, 2018, 7:35:09 PM4/6/18
to
My compiler didn't support Generic, but I've just done a quick
implementation, and compiling your code, it produced:

foo_0(000000000022FF48, blah0: 1, 4, 5)
foo_1(000000000022FF40, blah0: 2, blah1: 3, 4, 5)

Which looks the same as Melzzzzz's, apart from the addresses.

Interestingly, this mod only took 15 minutes, and 40 lines of code, and
while it's not finished and could do with more testing and more checks,
better type compares (and, probably, rewriting), it was enough to run
your code so the enhancements can wait.

This is the easy kind of language feature I like (compare with VLAs).

--
bartc

bartc

unread,
Apr 6, 2018, 7:40:54 PM4/6/18
to
On 07/04/2018 00:35, bartc wrote:
> On 04/04/2018 05:16, Chris M. Thomasson wrote:
>> On 3/31/2018 4:19 PM, Chris M. Thomasson wrote:
>>> So here is what I quickly came up with, keep in mind that I never
>>> really used _Generic before except with tgmath.h:
>>> _________________________________
>> [...]
>>> _________________________________
>>>
>>>
>>> As you can see, this code allows me to call the right implementation
>>> of foo, as foo_0 or foo_1 depending on the type of the first
>>> parameter. Now, can you see anything terribly wrong here? Is this
>>> Kosher?
>>>
>>> Thanks.
>>
>> Can you anybody else get it to compile, and run?
>
> My compiler didn't support Generic, but I've just done a quick
> implementation, and compiling your code, it produced:
>
> foo_0(000000000022FF48, blah0: 1, 4, 5)
> foo_1(000000000022FF40, blah0: 2, blah1: 3, 4, 5)

Incidentally, with other compilers, your program only worked on Pelles C
and gcc. Four other compilers failed to compile it. Maybe _Generic is
too new? I don't know.

Steven Petruzzellis

unread,
Apr 7, 2018, 5:39:55 AM4/7/18
to
Do you believe the lies jacobnavia is spewing? BTW, I've already told him that his use of "cult-like" to describe Shawn Ulman is mud slinging, since he's likening them to mindless automatons.

Demanding jacobnavia to admit guilt, which I have done, is not the same as showing his dishonesty. My main idea was that honest people show up here *to* assail those who disagree. It's not as if honest people don't know what this forum is, Shawn Ulman included.

You can say I am President Trump for all I care.

--
E-commerce Simplified!
https://www.youtube.com/playlist?list=PLU_evUKollmjeuJy5V5UMotPUs65Q7YM6
https://goo.gl/Fho5Nq
https://www.youtube.com/watch?v=AglvCo3dJ38&feature=youtu.be
Jonas Eklundh Communication AB

Chris M. Thomasson

unread,
Apr 7, 2018, 7:12:43 PM4/7/18
to
Thank you for all of your work wrt getting a quick working setup in your
own compiler. Also, yes, I can only get it to work on GCC and Pelles C
as well. Damn.

Chris M. Thomasson

unread,
Apr 8, 2018, 12:46:18 AM4/8/18
to
Seriously! Nobody else noticed the fact that:
___________
void foo_0(struct foo_0* self, int a, int b)
{
printf("foo_0(%p, blah0: %u, %u, %u)\n",
(void*)self, self->blah0, a, b);
}
__________

a and b from the parameters in the function call are still using
unsigned printf %u ? I left that in on purpose. Oh, by the way, it
exists in foo_1 as well... ;^)

To quickly fix change the type of the a and b parameters from int to
unsigned int in foo_0 and foo_1

I thought somebody would notice this by now.

> ;^o

asetof...@gmail.com

unread,
Apr 8, 2018, 2:08:22 AM4/8/18
to
Flying Spaghetti Monster
could be the program I now writing...
full of goto... Until now it not enter in a wrong goto loop...

Chris M. Thomasson

unread,
Apr 8, 2018, 2:23:34 AM4/8/18
to
Try to avoid goto when possible. :^)

Steven Petruzzellis

unread,
Apr 8, 2018, 2:43:29 AM4/8/18
to
How is synaptic connections tied to failed neuron connectivity in any way going to lead to valuable usenet writing? Reviewing LibreOffice... even now a neophyte... James Kuyper must know Rick C. Hodgin knows he is just being a moron!

Why would you want to restrict LibreOffice on Fedora to what can be done on Windows? It was James Kuyper who stated that he and his hired winos used to hack school computers all the time and it was just fun. The guy is as popular as ants at a picnic -- and with good cause.

--
This Trick Gets Women Hot For You
https://groups.google.com/forum/#!topic/comp.lang.c/KDQrv8appR8
https://prescottareapsychopaths.wordpress.com/mark-bilk-psychopath/
http://tinyurl.com/kmqe66h
Jonas Eklundh

Chris M. Thomasson

unread,
Apr 9, 2018, 6:52:07 PM4/9/18
to
I miss the good ol' days when a little code would be jumped on by all
sorts of corrections and/or better ways to code it. :^)
0 new messages