Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Token pasting (## operator) - Add whitespace to a token

Reply
Thread Tools

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

 
 
Wessi
Guest
Posts: n/a
 
      08-11-2005
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?

 
Reply With Quote
 
 
 
 
junky_fellow@yahoo.co.in
Guest
Posts: n/a
 
      08-11-2005

Wessi wrote:
> 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)

 
Reply With Quote
 
 
 
 
Wessi
Guest
Posts: n/a
 
      08-11-2005
Thanks, you're right. I had another problem with double macro
expansion, which made me believe, that your solution will not work.

 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      08-11-2005
On Thu, 11 Aug 2005 04:40:12 -0700, Wessi wrote:

> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
token pasting help (##) mark.bergman@thales-is.com C Programming 2 06-22-2007 06:33 PM
token pasting problem in K&R preprocessor Henry Townsend C Programming 13 06-24-2006 02:51 AM
Token pasting and what does the result need to be? Mark Odell C Programming 7 05-01-2006 09:24 PM
Token-pasting trouble Mark Odell C Programming 8 04-06-2006 04:12 PM
Templates, token-pasting, defines and literals... Jean-Claude Gervais C++ 3 06-27-2005 04:31 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57