0

Hello
I have to function

void lala_a(adktis *p)
void lala_k(kdktis *p)

These two functions are doing the same things but i want to make one function tha can accept adktis/kdktis something like this

void lala(adktis *p);
void lala(kdktis *p);

Is there a way to do this in c?
Thank you very much

3
Contributors
5
Replies
7
Views
5 Years
Discussion Span
Last Post by xxmp
1

Is there a way to do this in c?

In standard C, the short answer is no. The long answer is that you can do it with a lot of fuss and bother, but ultimately it isn't worth the effort for a weak facsimile of function overloading. The alternative answer is if you're willing to sacrifice the full behavior of overloading (ie. the correct function body is magically chosen), you can make a variant type using unions or use variable argument lists to hack something reasonably close to the end result. Once again, it's overly complicated for the minor convenience you get.

If you really want this behavior and your compiler supports overloading as an extension, that's the direction you should go.

Also, the next C standard has a feature in the works that would theoretically accomplish what you want:

#define lala(x) _Generic((x), adktis*: lala_a, kdktis*: lala_k)(x)

Not that it helps you now, of course. ;)

Votes + Comments
For the psychic poster
0

but ultimately it isn't worth the effort for a weak facsimile of function overloading.

Wow you got function overloading from the original posting? You must be psychic.

0

thank you for your answer. Should be good to do void lala(void * dktis)? Or this too generic to do it?
Thank you

Edited by xxmp: n/a

0

Should be good to do void lala(void * dktis)? Or this too generic to do it?

You still need a way to differentiate between adktis and kdktis.

0

So the best solution is that i did or the other you suggested? Is a way to make a new object that has type adktis or kdktis and put it as input in the function?
Thanks

Edited by xxmp: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.