C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Question about macros

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
vineoff
Guest





PostPosted: Sun Nov 27, 2005 10:45 pm    Post subject: Question about macros Reply with quote



How can macro take a number in this way (or can it) :

#define MYMACRO(x) (x)

int val = 5;

MYMACRO(val);

seems it produces val.

I need to have a macro that takes a string and a size_t and produces a
function name.

If I call it :

MYMACRO(function, val)();

it should call function5();

Is it possible?

Back to top
John Harrison
Guest





PostPosted: Sun Nov 27, 2005 10:56 pm    Post subject: Re: Question about macros Reply with quote



vineoff wrote:
Quote:
How can macro take a number in this way (or can it) :

#define MYMACRO(x) (x)

int val = 5;

MYMACRO(val);

seems it produces val.

Remember that macros work before you program is compiled so they cannot
know the value of a variable.

Quote:

I need to have a macro that takes a string and a size_t and produces a
function name.

If I call it :

MYMACRO(function, val)();

it should call function5();

Is it possible?


No. Macros know nothing about the values of variables.

Now whatever problem you are trying to solve, macros aren't the way to
do it. So explain what you are really trying to do, and someone will be
able to tell you the right way to do it.

John

Back to top
vineoff
Guest





PostPosted: Sun Nov 27, 2005 11:01 pm    Post subject: Re: Question about macros Reply with quote



Then I think it would be impossible to do but I'd need to read function
names from file and call them. May be a bug in design process but
anyway.

Back to top
John Harrison
Guest





PostPosted: Sun Nov 27, 2005 11:06 pm    Post subject: Re: Question about macros Reply with quote

vineoff wrote:
Quote:
Then I think it would be impossible to do but I'd need to read function
names from file and call them. May be a bug in design process but
anyway.


In C++ you can't do much better than this

if (name == "something")
{
something();
}
else if (name == "wotsit")
{
wotsit();
}

etc.

You can tart it up a little but essentially you have to check the name
and call the function.

Now if you were programming Java it would be a different matter, but C++
is not very good at this sort of thing.

john

Back to top
Bob Hairgrove
Guest





PostPosted: Sun Nov 27, 2005 11:09 pm    Post subject: Re: Question about macros Reply with quote

On 27 Nov 2005 14:45:58 -0800, "vineoff" <vineoff (AT) gmail (DOT) com> wrote:

Quote:
How can macro take a number in this way (or can it) :

#define MYMACRO(x) (x)

int val = 5;

MYMACRO(val);

seems it produces val.

I need to have a macro that takes a string and a size_t and produces a
function name.

If I call it :

MYMACRO(function, val)();

it should call function5();

Is it possible?

You can do this (sort of), but something tells me it isn't what you
need:

// test_macro.cpp
#define MYMACRO(function,val) function##val##(##)

#include <iostream>
#include <ostream>

void func1() { std::cout << "Called func1" << std::endl; }
void func2() { std::cout << "Called func2" << std::endl; }
void func3() { std::cout << "Called func3" << std::endl; }
void func4() { std::cout << "Called func4" << std::endl; }
void func5() { std::cout << "Called func5" << std::endl; }

int main()
{
MYMACRO(func,1);
MYMACRO(func,2);
MYMACRO(func,3);
MYMACRO(func,4);
MYMACRO(func,5);
}

Actually, you could do this much better with a template function, and
it would have the tremendous advantage over a macro of being typesafe!

The problem is probably that you need to choose a function dynamically
according to some runtime value. This macro can't do that because it
performs text substitution before the compiler ever gets around to
compiling your code.

To do this, you need function pointers and some kind of mapping from
strings (or integers, or whatever you use for a key) to function
pointers. std::map<> would serve you well, I believe.

--
Bob Hairgrove
[email]NoSpamPlease (AT) Home (DOT) com[/email]

Back to top
Markus Moll
Guest





PostPosted: Sun Nov 27, 2005 11:11 pm    Post subject: Re: Question about macros Reply with quote

Hi

vineoff wrote:

Quote:
Then I think it would be impossible to do but I'd need to read function
names from file and call them. May be a bug in design process but
anyway.

That's possible.
Use a map from function names to function pointers:

#include <iostream>
#include <string>
#include <map>
using namespace std;

void f1();
void f2();
void f3();
void f4();

int main()
{
map<string, void (*)()> func_map;
func_map["f1"] = &f1;
func_map["f2"] = &f2;
func_map["f3"] = &f3;
func_map["f4"] = &f4;

string name;
while(cin >> name)
{
(*func_map[name])();
}
}


Markus

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.