Connecting Tech Pros Worldwide Help | Site Map

A MACRO question

  #1  
Old July 22nd, 2008, 12:55 PM
ManicQin
Guest
 
Posts: n/a
Hi all.

Is there a way to iterate through a pre-defined list in pre-compile
time?
(not with for)

something like:

#define LIST int,double,float,string......
#define MAX 10
#define functionPattern(type) \
void return##type() { \
cout << #type << endl; \
}

//Replacing the next macro
#define functionCreator \
functionPattern(LIST[1]) \
functionPattern(LIST[2]) \
..
..
..
functionPattern(LIST[MAX])

Thanks
  #2  
Old July 22nd, 2008, 02:05 PM
puzzlecracker
Guest
 
Posts: n/a

re: A MACRO question


On Jul 22, 7:46*am, ManicQin <Manic...@gmail.comwrote:
Quote:
Hi all.
>
Is there a way to iterate through a pre-defined list in pre-compile
time?
(not with for)
>
something like:
>
#define LIST int,double,float,string......
#define MAX 10
#define functionPattern(type) \
void return##type() { \
cout << #type << endl; \
>
}
>
//Replacing the next macro
#define functionCreator *\
functionPattern(LIST[1]) \
functionPattern(LIST[2]) \
.
.
.
functionPattern(LIST[MAX])
>
Thanks


Please explain what this means:

void return##type() { \
cout << #type << endl; \
}

Is it even legal C++ code?
  #3  
Old July 22nd, 2008, 02:05 PM
Pete Becker
Guest
 
Posts: n/a

re: A MACRO question


On 2008-07-22 08:57:16 -0400, puzzlecracker <ironsel2000@gmail.comsaid:
Quote:
On Jul 22, 7:46Â*am, ManicQin <Manic...@gmail.comwrote:
Quote:
>Hi all.
>>
>Is there a way to iterate through a pre-defined list in pre-compile
>time?
>(not with for)
>>
>something like:
>>
>#define LIST int,double,float,string......
>#define MAX 10
>#define functionPattern(type) \
>void return##type() { \
>cout << #type << endl; \
>>
>}
>>
>//Replacing the next macro
>#define functionCreator Â*\
>functionPattern(LIST[1]) \
>functionPattern(LIST[2]) \
>.
>.
>.
>functionPattern(LIST[MAX])
>>
>Thanks
>
>
>
Please explain what this means:
>
void return##type() { \
cout << #type << endl; \
}
>
Is it even legal C++ code?
No, but that's not the full text of the macro. The full text is:


#define functionPattern(type) \
void return##type(){\
cout << #type << endl;\
}

and, yes, it's legal. return##type concatenates "return" and the
replacement text for "type" into a single token. And #type is simply
the replacement text for "type" surrounded by quotes. So, for example:

functionPattern(foo)

would expand to

void returnfoo(){
cout << "foo" << endl;
}

Try it.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

  #4  
Old July 22nd, 2008, 02:35 PM
ManicQin
Guest
 
Posts: n/a

re: A MACRO question


On Jul 22, 1:54*pm, "Alf P. Steinbach" <al...@start.nowrote:
Quote:
Yes, the Boost library provides some sophisticated (really unbelievable) macro
magic.
Thanks I looked at how Boost do it and because I cant use Boost
I could only steal their code... but it's too much work more work
than I can afford to spent on just saving few lines...


Thanks!

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro question Peithon answers 8 May 30th, 2007 10:35 PM
Macro Question Andrew Wied answers 1 November 22nd, 2005 11:29 AM
Macro Question Andrew Wied answers 1 November 18th, 2005 11:57 AM
C-preprocessor macro question Charlie Zender answers 3 November 14th, 2005 12:39 AM
Macro Question Andrew Wied answers 1 July 21st, 2005 03:39 PM