By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,206 Members | 1,035 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,206 IT Pros & Developers. It's quick & easy.

Question about macros

P: n/a
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?

Nov 27 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
vineoff wrote:
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
Nov 27 '05 #2

P: n/a
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.

Nov 27 '05 #3

P: n/a
vineoff wrote:
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
Nov 27 '05 #4

P: n/a
On 27 Nov 2005 14:45:58 -0800, "vineoff" <vi*****@gmail.com> wrote:
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
No**********@Home.com
Nov 27 '05 #5

P: n/a
Hi

vineoff wrote:
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
Nov 27 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.