468,513 Members | 1,759 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

Dynamic #define

Hi,

I currently have a application which needs to dynamically define a
function (post-compilation). In Lisp, this can be done by embedding a
function definition inside a string, and call eval(string). In C++, I
thought about using #define macros, like so:

#define some_function() int a = 3; int b = 5; cout << a + b << endl;

int main(int argc, char *argv[]) {
some_function();
return 0;
}

main() in this case would print out "8". The problem is that I don't
know the function definition before hand, and I need something like:

#define some_function() STRING

where STRING contains "int a = 3; int b = 5; cout << a + b <<
endl" (which can be parsed from a file, for example).

Does anyone know how to do this?

Thanks!
Jan 12 '08 #1
2 6772

<ji*******@gmail.comwrote in message
news:50**********************************@i12g2000 prf.googlegroups.com...
Hi,

I currently have a application which needs to dynamically define a
function (post-compilation).
[snip]

defines are read at pre-compile time and the word dynamic usually means
"changes at runtime", so a dynamic defines are not possible.
It sounds to me that you need to use function pointers or functors
where STRING contains "int a = 3; int b = 5; cout << a + b <<
endl" (which can be parsed from a file, for example).
[snip]

This sounds like you need a factory of functors

Both "design pattern factory" and "C++ functor" can be googled, so I won't
elaborate too much. Hope that helps!
Jan 12 '08 #2
In article <5043ad3d-672f-4f3d-ab9c-051562783465
@i12g2000prf.googlegroups.com>, ji*******@gmail.com says...
Hi,

I currently have a application which needs to dynamically define a
function (post-compilation). In Lisp, this can be done by embedding a
function definition inside a string, and call eval(string).
[ ... code elided ]
Does anyone know how to do this?
C++ doesn't provide any direct support for this as part of the language
itself. You have a couple of choices. One is to write a lexer/parser and
evaluate the string yourself. Another that's applicable in some
environments via non-portable code is to invoke the C++ compiler to
create a dynamic library, then dynamically link to that library. The
details of that will depend on your specific environment though.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jan 13 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by K | last post: by
4 posts views Thread by Scott Lyons | last post: by
6 posts views Thread by Materialised | last post: by
3 posts views Thread by Stephen Gennard | last post: by
8 posts views Thread by George Meng | last post: by
2 posts views Thread by Luis Arvayo | last post: by
10 posts views Thread by jflash | last post: by
reply views Thread by DennieOMP | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.