concatenating in a macro

From: Gergely Buday (gergoe_at_math.bme.hu)
Date: 09/27/04


Date: 27 Sep 2004 12:34:47 -0700

Hi folks,

c++ faq lite writes:

However you need a double layer of indirection when you use ##.
Basically you need to create a special macro for "token pasting" such
as:

 #define name2(a,b) name2_hidden(a,b)
 #define name2_hidden(a,b) a ## b

Trust me on this --- you really need to do this! (And please nobody
write me saying it sometimes works without the second layer of
indirection. Try concatenating a symbol with __LINE__ and see what
happens then.)

---
Now my code is 
#include <stdio.h>
 
#define MYMACRO(a,b) \
        a ## b
 
int main(int argc, char** argv)
{
  int MYMACRO(tmp, __LINE__) = 1;
  printf("%d\n", MYMACRO(tmp, __LINE__));
  return 0;
}
 
and it compiles (with g++) and runs smoothly. What does the faq author
talk about?
Thanks in advance
- Gergely


Relevant Pages

  • Re: Taking a single large .cs file and making many smaller .cs fil
    ... If this were true why have any indirection at all, ... Oriented even if written in an OO language, functionality and code is ... "Adding a layer of indirection always creates more ... if there are multiple files then it's harder to keep ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Taking a single large .cs file and making many smaller .cs files
    ... "Adding a layer of indirection always creates more ... I guess I feel that progressively breaking the problem down into ... down smaller we're actually hindering comprehension. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Refactoring all the time
    ... and then implement yet another object-handling system ... Are you suggesting that there is something wrong with indirection? ... The most perfect game would have layer upon layer of abstraction, ... description of what a roguelike is. ...
    (rec.games.roguelike.development)
  • Re: OffTopic: Wie kann ich am besten Source (formatiert) posten?
    ... Thomas ... Any problem in computer science can be solved with another layer ... of indirection. ...
    (microsoft.public.de.vb)