LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Any (standard) way to have anonymous functions in C? (https://www.linuxquestions.org/questions/programming-9/any-standard-way-to-have-anonymous-functions-in-c-4175460707/)

Gullible Jones 05-04-2013 02:35 PM

Any (standard) way to have anonymous functions in C?
 
I have a program I'm working on, in which I'd like to have structures containing pointers to anonymous functions. Basically the structure type would contain a function pointer as a member; and when each individual structure was initalized, I would feed the address of an anonymous function into its pointer.

This way I could have some degree of polymorphism; i.e. I could call the correct function required by a given structure instance, without needing different function names, switch statements, etc. Instead of calling foo() for a foo_s struct and bar() for a bar_s struct, I could call (some_foo->func)() or (some_bar->func)().

Is there a sane, commonplace way to do this in C? Is it even possible without implementing half of C++? Failing that, are there better ways ot have this kind of polymorphism?

psionl0 05-04-2013 06:17 PM

http://stackoverflow.com/questions/8...ters-in-c-work

Gullible Jones 05-04-2013 06:43 PM

Thanks very much, but that doesn't seem to help a whole lot. With just function pointers, I would still have to implement a large number of named functions.

Is there any way to create anonymous functions in C, that is not dependent on the compiler?

psionl0 05-04-2013 06:47 PM

You still have to write the functions anyway. You could call them classA_function1, classA_function2, classB_function1 etc. If you really want them to be anonymous then you might as well use C++.

Gullible Jones 05-04-2013 06:50 PM

I wanted them to be anonymous because that could allow for prettier, more readable code. (Or so I thought anyway.) I can't use C++ without porting the entire existing code base.

Thanks though!

dugan 05-04-2013 06:57 PM

No, there is no standard way to have anonymous functions in C.

The Wikipedia article on anonymous functions does have a gcc example though.

http://en.wikipedia.org/wiki/Anonymo...da_expressions

Quote:

This way I could have some degree of polymorphism; i.e. I could call the correct function required by a given structure instance, without needing different function names, switch statements, etc. Instead of calling foo() for a foo_s struct and bar() for a bar_s struct, I could call (some_foo->func)() or (some_bar->func)().
You don't need anonymous functions for that.

Gullible Jones 05-04-2013 07:16 PM

I know I don't need them, it just seemed like a good idea for keeping the code more compact.

Sergei Steshenko 05-05-2013 08:02 AM

Quote:

Originally Posted by Gullible Jones (Post 4944897)
I have a program I'm working on, in which I'd like to have structures containing pointers to anonymous functions. Basically the structure type would contain a function pointer as a member; and when each individual structure was initalized, I would feed the address of an anonymous function into its pointer.

This way I could have some degree of polymorphism; i.e. I could call the correct function required by a given structure instance, without needing different function names, switch statements, etc. Instead of calling foo() for a foo_s struct and bar() for a bar_s struct, I could call (some_foo->func)() or (some_bar->func)().

Is there a sane, commonplace way to do this in C? Is it even possible without implementing half of C++? Failing that, are there better ways ot have this kind of polymorphism?


C99 standard does not have anonymous functions.

mina86 05-06-2013 01:31 AM

Others have already answered your question so I'll just add that the usual way is to declare those functions static so they are not exported to other translation units. But yes, you'll have to give them names, but as psionl0 pointed, this does not have to be that bad since they can be named after the callback name.

pan64 05-06-2013 02:54 AM

I do not really understand it, it is exactly how c++ works: you will have classes, member functions, you can also have virtual functions, and inheritance and finally this will do exactly what you need.
You could call always the correct function because every class instance will "know" himself.
I do not really understand why do you want to build a similar structure (reinvent the wheel?), it is already working and available.

Otherwise (without C++, in c) you must implement all those functions one by one with some names. You can than put them (or their pointers) in an array, shared lib or whatever you want, and use them.

The third possibility could have been a c++ to c translator...


All times are GMT -5. The time now is 08:03 AM.