1

All I'm trying to do is use a macro to generate class names, it needs a bit of concat, and that's it. Except it fails miserably. This is really grinding my gears.

I have a macro defined before somewhere...

#define CLASSNAME myclassname
...

and I'm trying to get a generated classname with a type...

#define GETNAME(x) x
#define UNIQUENAME(T) GETNAME(CLASSNAME) ## _ ## T

UNIQUENAME(int)   //I want it to make: myclassname_int
                  // instead it makes: myclassname _int
// SUBTLE, but screws everything up! can't have that space in the middle.

I checked another configuration...

#define UNIQUENAME(T) GETNAME(CLASSNAME)M ## M_ ## T
//which returns: myclassname MM_int

So the space definitely comes from the GETNAME result. Only thing is, I have no clue how to get rid of it. I've tried for way too long now.

Anything will help. Thanks!

extracrispy
  • 669
  • 5
  • 16
  • If you are using C++ then do not tag it as C. If you are using C++ then you can avoid the use of macros. Then you can use the saftely of the compiler that is better at these things. Have you heard of templates? Probably the best solution for your problem if I understand you correctly – Ed Heal Aug 30 '13 at 02:13
  • Yep, that is exactly what I want. Templates. Unfortunately, I'm using a compiler for an embedded processor that only supports a subset of c++, and no templates. So I'm making them with macros. I have everything working except for that stupid space in the middle of the generated classname. As far as I can tell the preprocessor is fairly standard (aside from the space). – extracrispy Aug 30 '13 at 02:30
  • Instead of going down the route of using macros, write a script (perl etc) to generate the header and source files as desired for the various types. It would be just a simple substitution. – Ed Heal Aug 30 '13 at 02:49

1 Answers1

1
#define ClassName       myclassname

#define Paste(a, b)     a ## _ ## b
#define Helper(a, b)    Paste(a, b)
#define UniqueName(T)   Helper(ClassName, T)

UniqueName(int)

Here is an explanation of macro expansion and why we need helper macros like this.

Community
  • 1
  • 1
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • changing to UNIQUENAME(T) UNIQUENAMEHelper(GETNAME(CLASSNAME),T) returns the almost correct result, but it has the stupid space. "myclassname _int" – extracrispy Aug 30 '13 at 02:33