Macro increase value and then concatenate


I want to create a recursive Macro the will create the "next" class.


#define PRINTME(indexNum) class m_##(indexNum+1) { }

The indexNum + 1 is evaluated as an int, and won't concatenate to the class name.

How can I cause the compiler to evaluate that, before concatenating?


The simple answer is that you can't. The preprocessor generally deals in text and tokens; the only place arithmetic is carried out in in #if and #elif directives.

Also, macro expansion isn't recursive. During expansion, the macro being expanded is disabled, and is not available for further substitution.


If you want to generate unique class names every time the PRINTME is invoked then, following is one way:

#define CONCATE1(X,Y) X##Y #define CONCATE(X,Y) CONCATE1(X,Y) #define PRINTME class CONCATE(m_,__COUNTER__) {}

__COUNTER__ is an extension in gcc and I am not sure if it's present in other compilers. It's guaranteed that compiler will add 1 every time this macro is invoked.<br /> (In this case, you cannot use __LINE__ or __FILE__ effectively.)

<a href="http://ideone.com/m3s2t" rel="nofollow">Demo</a>.


Well it is doable, based on your motivation and ability to endure ugly code. First off define increment macro:

#define PLUS_ONE(x) PLUS_ONE_##x #define PLUS_ONE_0 1 #define PLUS_ONE_1 2 #define PLUS_ONE_2 3 #define PLUS_ONE_3 4 #define PLUS_ONE_4 5 #define PLUS_ONE_5 6 #define PLUS_ONE_7 8 #define PLUS_ONE_8 9 #define PLUS_ONE_9 10 // and so on...

You can't just use PLUS_ONE(x) in concatenation operation, since preprocessor won't expand it. There is a way, however - you can abuse the fact that the preprocessor expands variadic arguments.

// pass to variadic macro to expand an argument #define PRINTME(indexNum) PRINTME_PRIMITIVE(PLUS_ONE(indexNum)) // do concatenation #define PRINTME_PRIMITIVE(...) class m_ ## __VA_ARGS__ { }


PRINTME(1); // expands to class m_2 { };

Have you considered using templates instead?


  • Multiselect using mouse in TListview?
  • Using the Built in Visual Studio Diff Tool to View P4VS's Diffs
  • How do I make Paypal to send IPN to my server with simple HTTP authentication?
  • selenium server launching two test execution tabs in firefox
  • “the type does not fulfill the required lifetime” when using a method in a thread
  • How to escape wildcard expansion in a variable in bash?
  • Bash : Adding extra single quotes to strings with spaces
  • How to get rid of enclosing double-quotes in the expanded `forfiles` variables?
  • Sphinx substitution with a container
  • Bash expand variable containing sed pattern correctly
  • Brackets ${}, $(), $[] difference and usage in bash [duplicate]
  • Call objective C macro from swift
  • Can I put condition in emacs lisp macro?
  • Event difference bootstrap collapse
  • Get text between two elements JQUERY
  • GCC error: Cannot apply offsetof to member function MyClass::MyFunction
  • Is co-variance safe here?
  • const char **a = {“string1”,“string2”} and pointer arithametic
  • functions and when to use brackets/parenthesis
  • Having trouble creating a list based on function arguments
  • how to know the last row filled in vba (excel)? [duplicate]
  • javascript add operation returns bad result
  • How to check if DIV element is disabled using jquery
  • Change navbar in bootstrap if user login
  • IE11 textarea loses focus if another textarea is disabled
  • Most efficient way to replace lowest list values in dataframe in R
  • VBA Dir function not working on Excel 2010
  • How can I reset dropdown data if modal closed on vue component?
  • Let a function return any type in C++ class
  • How to getText() from the input field of an angularjs Application
  • bad substitution shell- trying to use variable as name of array
  • Object and struct member access and address offset calculation
  • Z3: Convert between FP and BitVector?
  • What does 'Language neutral' mean with regard to MAKELANGID?
  • Excel's Macro-Recorder usage
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • How to use carriage return with multiple line?
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Weird JavaScript statement, what does it mean?
  • need help with bizarre java.net.HttpURLConnection behavior