Connecting Tech Pros Worldwide Help | Site Map

A MACRO question

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2008, 11:55 AM
ManicQin
Guest
 
Posts: n/a
Default A MACRO question

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, 01:05 PM
puzzlecracker
Guest
 
Posts: n/a
Default 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, 01:05 PM
Pete Becker
Guest
 
Posts: n/a
Default 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, 01:35 PM
ManicQin
Guest
 
Posts: n/a
Default 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!

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.