• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Creating anonymous functions?

## Recommended Posts

eighty    220
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)() = ???


##### Share on other sites
drslush    122
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?

##### Share on other sites
Mastaba    761
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.

##### Share on other sites
eighty    220
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?

##### Share on other sites
Mastaba    761
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.

##### Share on other sites
Guest Anonymous Poster
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!

##### Share on other sites
Way Walker    745
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.

##### Share on other sites
eighty    220
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?

##### Share on other sites
Guest Anonymous Poster
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.

##### Share on other sites
leroy    122
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.

##### Share on other sites
eighty    220
quote:
Original post by Anonymous Poster
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.

Interesting... I'll check it out.

quote:

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

Hm... I was under the impression gcc == g++...?

Thanks for the replies.

[edited by - eighty on July 9, 2003 2:38:38 AM]

##### Share on other sites
Guest Anonymous Poster
quote:
Original post by eighty
Hm... I was under the impression gcc == g++...?

Most (all?) extensions are language specific. AFAIK, all the languages supported by GCC are compiled to a Lisp-like intermediate code by the language specific frontend. The resulting code is then compiled by the compiler backend. Therefore, the C++ frontend will likely not be able to handle a C extension, and vice-versa.

I could be wrong of course: I don''t know GCC''s internals, so feel free to correct me.

Hope this helps.

##### Share on other sites
eighty    220
Umm... I meant I thought gcc was the same thing as g++. Just another name for it. I guess I was wrong.

##### Share on other sites
daerid    354
gcc used to stand for GNU C Compiler. Now, it stands for GNU Compiler Collection, and includes the C compiler (gcc), the C++ compiler (g++), the pascal compiler, as well as a couple others (I forget exactly which ones).

##### Share on other sites
Shannon Barber    1681
C, C++, Object C, Fortran, Java, and Ada are supported by gnu, and there are many other front-ends available.