0

I want to concatenate a C type name after some identifier. That's straightforward if the type name consists of just one token. However, if it's for example long int, I'm not finding a working solution.

For example, this code:

#define ADDSUFFIX(X,Y) X ## Y
#define EVALADDSUFFIX(X,Y) ADDSUFFIX(X,Y)
#define COMBINE(X,Y) EVALADDSUFFIX(X,EVALADDSUFFIX(__,Y))

#define SIMPLETEST COMBINE(myid,long int)

#define XSTR(x) STR(x)
#define STR(x) #x

#pragma message "You get this: " XSTR(SIMPLETEST)

Provides this as output:

./testing.h:35:9: note: '#pragma message: You get this: myid__long int'

So, obviously, the whitespace is there, and I don't get a token, but a couple of tokens.

Is it possible to either remove the whitespace, or replace it with an underscore for example? (both solutions would be fine, either myid__longint or myid__long_int).

cesss
  • 852
  • 1
  • 6
  • 15
  • 2
    Do not seek to play games with the preprocessor. It is not designed for things like this. Look for another way to accomplish the goal. – Eric Postpischil Nov 29 '20 at 21:16
  • 1
    My take: avoid the problem with a `typedef long int longint;` and use the typedef everywhere. – Jens Nov 29 '20 at 21:48
  • 1
    What, exactly, is the point? If you want to decorate an identifier with type information (a dubious practice, if you ask me), then what do you think you gain from involving the preprocessor in doing so? I am inclined to think that all the needed macro goop would more than overcome any advantage from decorating the identifiers. – John Bollinger Nov 29 '20 at 22:41

1 Answers1

0

I think this is not possible without separating long and int into different arguments, while using __VA_ARGS__ in the preprocessor and probably an iterator technique to process all of them.

To do so, you can do your own (with a maximum of 2 tokens it may be easier than infinite, take a look at How to write a while loop with the C preprocessor? ) or use an external library like BOOST.

Edit

I found an interesting link about recursion with preprocessor which may help construct the macro.

emi
  • 2,786
  • 1
  • 16
  • 24