Question about macros

V

vineoff

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?
 
J

John Harrison

vineoff said:
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.
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
 
V

vineoff

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.
 
J

John Harrison

vineoff said:
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
 
B

Bob Hairgrove

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.
 
M

Markus Moll

Hi
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top