Creating anonymous functions?

Started by
14 comments, last by eighty 20 years, 9 months ago
Is it possible to create functions without names, just like you can create objects without names? So that you can have an array of functions without real names that clutter up the global (or any) namespace.

void func() { /*blah*/ }

// instead I want something like:


void (*pFunc)() = ???
Advertisement
If I understand your question, then the answer is no, you can''t. But if you are worrying about cluttering the global namespace, then just put your functions in a seperate namespace. Is that a problem?
Well, technically, yes it is possible to do so at runtime. Allocate some memory and fill it with the machine code for your functions, and create an array that contains function pointers that point to the various function entry points.
.
Hmm... how would I go about doing that? I suppose it''s not directly supported by the language, so it wouldn''t be pretty, I guess?
There are some things to keep in mind when creating such code. It must be relocatable code, since you don''t know before hand where it will absolutely be in memory. Or if is isn''t relocatable, then you must patch it up which would be much more trouble than its worth. If the only reason you want to do this is to not pollute the global namespace I wouldn''t do this. Just create a namespace for them.
.
C and C++ do not treat functions as first-class data.

You can malloc() a bunch of memory and generate machine code into this memory; this is not at all supported by the language and will look different for each type of CPU architecture you intend to run on (PPC, x86, Itanium, etc).

THEN you have to make sure that you flush/invalidate the D-cache and I-cache for the malloc-ed area, else bad things might happen when you try to jump to it :-) Some OS-es also require that you map the area "executable" which it might not be by default. These two operations are OS dependent (Windows, MacOS, Linux, etc).

Thus, you have the cross section of possible CPUs and possible OSes to deal with. Delicious!
Your best bet is to use a static function (or unnamed namespace for you C++ people). The name will be local to this translation unit, so you''re only cluttering the current namespace.

As for the filling an array with machine instructions, there''s a reason the standard doesn''t guarantee you can cast a function pointer to a void pointer. It either cannot work or is very difficult on some systems. Of course, that''s a matter of portability. It''ll rule out some platforms and may require significant work to port it to others. It''s a trade off.

Another option would be to interface with a language with first class functions. For example, from what I can gather, Haskell (with the FFI adendum) allows conversion of its functions to C functions at runtime. This may or may not be wise. (using multiple languages isn''t something to take lightly, but shouldn''t be ruled out)

Also, if you''re using gcc and don''t care about other compilers, you can make functions local to a function. Thus, you only clutter the namespace within the function (not a big deal).

Depending on why you need this, you might even be able to find another, simpler solution.
Yeah I''ll probably go with an unnamed namespace.

Too bad lambda functions aren''t supported in C++ though.

I basically need to run (and possibly create) particular functions runtime, so I need to map functions with strings. Since I won''t ever call a function explicitly in code I don''t need or want them to have names.

Does gcc support nested functions?
quote:Original post by eighty
Yeah I''ll probably go with an unnamed namespace.

Too bad lambda functions aren''t supported in C++ though.

I basically need to run (and possibly create) particular functions runtime, so I need to map functions with strings. Since I won''t ever call a function explicitly in code I don''t need or want them to have names.


Check out Boost Lambda (http://www.boost.org). It''s fairly limited and not nearly as flexible as lambda constructs in (mostly) functional languages, but if your needs are simple, it might be what you''re after.

quote:
Does gcc support nested functions?


Gcc does, but not g++, AFAIK. It only works as an extension to the C language.

Given your needs for lambda constructs and nested functions, I''m wondering if you''re using the right language for the job though...

Hope this helps.
I haven''t really tried it yet, but the boost library implements lambda functions: http://www.boost.org/libs/lambda/doc/index.html Boost contains some other useful stuff too. I don''t know if it''s suited for your problem, though.

This topic is closed to new replies.

Advertisement