Token pasting (## operator) - Add whitespace to a token

W

Wessi

Hi,

token pasting means, that normally whitespaces and comments are deleted
before and after the ## operator. I want the preprocessor to hold an
existing whitespace while replacing the argument of a macro. Example:

The following define and macro

#define ISR_CAT1(NAME) __interrupt void ##NAME(void)

ISR_CAT1(myIsrHandler)
{

}

should be expanded to

__interrupt void myIsrHandler(void)
{

}

But the preprocessor does the following replacement (deleting
whitespaces before ##):

__interrupt voidmyIsrHandler(void)
{

}

Is there any possibility to avoid this kind of replacement/deleting the
whitespaces in front of ## operator?
 
J

junky_fellow

Wessi said:
Hi,

token pasting means, that normally whitespaces and comments are deleted
before and after the ## operator. I want the preprocessor to hold an
existing whitespace while replacing the argument of a macro. Example:

The following define and macro

#define ISR_CAT1(NAME) __interrupt void ##NAME(void)

ISR_CAT1(myIsrHandler)
{

}

should be expanded to

__interrupt void myIsrHandler(void)
{

}

But the preprocessor does the following replacement (deleting
whitespaces before ##):

__interrupt voidmyIsrHandler(void)
{

}

Is there any possibility to avoid this kind of replacement/deleting the
whitespaces in front of ## operator?

Why are you using "##" ? Following definition should work.
#define ISR_CAT1(NAME) __interrupt void NAME(void)
 
W

Wessi

Thanks, you're right. I had another problem with double macro
expansion, which made me believe, that your solution will not work.
 
L

Lawrence Kirby

Hi,

token pasting means, that normally whitespaces and comments are deleted
before and after the ## operator. I want the preprocessor to hold an
existing whitespace while replacing the argument of a macro. Example:

The result of token pasing must be a valid pp-token and only pp-tokens
like character constants and string literals can contain spaces. Trying to
create another type of pp-token containing spaces doesn't make any sense.
The following define and macro

#define ISR_CAT1(NAME) __interrupt void ##NAME(void)

ISR_CAT1(myIsrHandler)
{

}

should be expanded to

__interrupt void myIsrHandler(void)
{

}

Token pasting is used to create new (composite) tokens but there aren't
any such tokens in your desired expansion; your error is in trying to use
token pasting when it is not appropriate. Use simply:

#define ISR_CAT1(NAME) __interrupt void NAME(void)
But the preprocessor does the following replacement (deleting
whitespaces before ##):

__interrupt voidmyIsrHandler(void)
{

}

Yes, your macro tries to paste the tokens void and myIsrHandler into a
single one whereas what you really need is just to leave them as separate
tokens.
Is there any possibility to avoid this kind of replacement/deleting the
whitespaces in front of ## operator?

Don't use token pasting.

Lawrence
 

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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top