# Another tricky c++ preprocessor question

**URL:** https://boards.straightdope.com/t/another-tricky-c-preprocessor-question/306454
**Category:** Factual Questions
**Created:** [June 1, 2005, 9:35pm UTC](https://boards.straightdope.com/t/another-tricky-c-preprocessor-question/306454 "2005-06-01T21:35:36Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![MaxTheVool](https://avatars.discourse-cdn.com/v4/letter/m/9fc29f/32.png) [@MaxTheVool](https://boards.straightdope.com/u/MaxTheVool)
#### Post date: [June 1, 2005, 9:35pm UTC](https://boards.straightdope.com/t/another-tricky-c-preprocessor-question/306454/1 "2005-06-01T21:35:36Z")

</div>

I want to set up a macro which #defines identifiers to sequentially higher integers.

For instance:

COUNTINGMACRO(DEAD\_STATE)  
COUNTINGMACRO(ALIVE\_STATE)  
COUNTINGMACRO(HAPPY\_STATE)  
COUNTINGMACRO(SAD\_STATE)

will have the same result as  
#define DEAD\_STATE 0  
#define ALIVE\_STATE 1  
#define HAPPY\_STATE 2  
#define SAD\_STATE 3

(And yes, I would normally use an enum, but this is a special case.)

Thoughts? Is this even possible?

thanks

---

<div class="post-metadata">

### Author: ![MaxTheVool](https://avatars.discourse-cdn.com/v4/letter/m/9fc29f/32.png) [@MaxTheVool](https://boards.straightdope.com/u/MaxTheVool)
#### Post date: [June 1, 2005, 9:38pm UTC](https://boards.straightdope.com/t/another-tricky-c-preprocessor-question/306454/2 "2005-06-01T21:38:40Z")

</div>

Oh, and if it helps, I have control over how the preprocessor is called, since this is actually being used for a scripting language. So I could, potentially, call the preprocessor multiple times if that would help.

(I think I have a solution that would work if I ran the preprocessor twice and could figure out how to make a macro resolve with line feeds in it.)

---

<div class="post-metadata">

### Author: ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)
#### Post date: [June 1, 2005, 9:41pm UTC](https://boards.straightdope.com/t/another-tricky-c-preprocessor-question/306454/3 "2005-06-01T21:41:01Z")

</div>

No can do. You can’t invoke macros inside another macro.

---

<div class="post-metadata">

### Author: ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)
#### Post date: [June 1, 2005, 9:44pm UTC](https://boards.straightdope.com/t/another-tricky-c-preprocessor-question/306454/4 "2005-06-01T21:44:29Z")

</div>

…of course, you don’t necessarily want to have a #define in there, you just want the same effect. I don’t know if you can get the preprocessor to run twice, but mutli-line macros are easy.

```auto

#define FOO( a, b ) \
	{ \
		foo(a); \
		foo(b); \
	}

```

You don’t necessarily need to enclose the whole thing in brackets, but it’s a good practice. Otherwise, you may start finding really weird errors.

Just out of curiosity, why can’t you use an enum?

---

<div class="post-metadata">

### Author: ![MaxTheVool](https://avatars.discourse-cdn.com/v4/letter/m/9fc29f/32.png) [@MaxTheVool](https://boards.straightdope.com/u/MaxTheVool)
#### Post date: [June 1, 2005, 9:51pm UTC](https://boards.straightdope.com/t/another-tricky-c-preprocessor-question/306454/5 "2005-06-01T21:51:19Z")

</div>

> [@ultrafilter](#):
>
> …of course, you don’t necessarily want to have a #define in there, you just want the same effect. I don’t know if you can get the preprocessor to run twice, but mutli-line macros are easy.
> 
> ```auto
> 
> #define FOO( a, b ) \
> { \
> foo(a); \
> foo(b); \
> }
> 
> ```
> 
> You don’t necessarily need to enclose the whole thing in brackets, but it’s a good practice. Otherwise, you may start finding really weird errors.
> 
> Just out of curiosity, why can’t you use an enum?

Your macro will resolve to { foo(a); foo(b); } all on one line. the \ means “treat what’s on the next line as if it’s on this line”.  
I can’t use an enum because enums are not part of the preprocessor, they’re part of the actual compiler. And I’m just using the preprocessor to provide macro support for a scripting language, which has its own syntax and compiler. I can add support for this feature directly into the scripting language, but that’s hard, whereas getting the preprocessor to do it for me should be easy.

> [@UltraFilter](#):
>
> No can do. You can’t invoke macros inside another macro.

The fact that I can run the preprocessor twice if I need to gets me partially around that.

Here’s what I tried first:

```auto

#define NEXT_COUNTER 0

 #define TEST_COUNTER(foo) \
#define foo NEXT_COUNTER \
#undef NEXT_COUNTER \
#define NEXT_COUNTER foo + 1

```

If I run this through the preprocessor twice, it ALMOST works. Except that instead of resolving to

```auto

#define foo NEXT_COUNTER
#undef NEXT_COUNTER
#define NEXCOUNTER foo + 1

```

it resolves to

```auto

#define foo NEXT_COUNTER #undef NEXT_COUNTER #define NEXCOUNTER foo + 1

```

---

<div class="post-metadata">

### Author: ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)
#### Post date: [June 1, 2005, 9:55pm UTC](https://boards.straightdope.com/t/another-tricky-c-preprocessor-question/306454/6 "2005-06-01T21:55:59Z")

</div>

I think you’re pretty much stuck if you want to use the standard preprocessor. You can always try writing your own…

There might also be something you can do with templates to get the effect you want, but I’m not 100% sure on that.

---

<div class="post-metadata">

### Author: ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)
#### Post date: [June 1, 2005, 10:04pm UTC](https://boards.straightdope.com/t/another-tricky-c-preprocessor-question/306454/7 "2005-06-01T22:04:04Z")

</div>

You know, the template approach might have some merit. I need to think about it some more cause the syntax gets ugly as sin. Take a look at Wikipedia’s [article on template metaprogramming](http://en.wikipedia.org/wiki/Template_metaprogramming) and see if you’re inspired to do something.

---

<div class="post-metadata">

### Author: ![JohnM](https://avatars.discourse-cdn.com/v4/letter/j/848f3c/32.png) [@JohnM](https://boards.straightdope.com/u/JohnM)
#### Post date: [June 1, 2005, 10:24pm UTC](https://boards.straightdope.com/t/another-tricky-c-preprocessor-question/306454/8 "2005-06-01T22:24:51Z")

</div>

Since C++ is AFAIK backwards compatible with ANSI (or K&R for that matter), how about using enum instead?

```auto

#include <stdlib.h>
#include <stdio.h>

enum {DEAD_STATE,
      ALIVE_STATE,
      HAPPY_STATE,
      SAD_STATE};

void main(void)
{
  printf("DEAD is %d
"
         "ALIVE is %d
"
         "HAPPY is %d
"
         "SAD is %d
",
         DEAD_STATE,
         ALIVE_STATE,
         HAPPY_STATE,
         SAD_STATE);
}

```

This yields the output:

> [@enumtest](#):
>
> DEAD is 0  
> ALIVE is 1  
> HAPPY is 2  
> SAD is 3

---

<div class="post-metadata">

### Author: ![JohnM](https://avatars.discourse-cdn.com/v4/letter/j/848f3c/32.png) [@JohnM](https://boards.straightdope.com/u/JohnM)
#### Post date: [June 1, 2005, 10:35pm UTC](https://boards.straightdope.com/t/another-tricky-c-preprocessor-question/306454/9 "2005-06-01T22:35:44Z")

</div>

And for your obfuscation pleasure, you can do it with a macro, just with the enum hidden behind the scenes:

```auto

#include <stdlib.h>
#include <stdio.h>

#define TABSTART enum{
#define TABEND ENDTAB};
#define PSEUDOCOUNTINGMACRO(v) v,

TABSTART
PSEUDOCOUNTINGMACRO(DEAD_STATE)
PSEUDOCOUNTINGMACRO(ALIVE_STATE)
PSEUDOCOUNTINGMACRO(HAPPY_STATE)
PSEUDOCOUNTINGMACRO(SAD_STATE)
TABEND

void main(void)
{
  printf("DEAD is %d
"
         "ALIVE is %d
"
         "HAPPY is %d
"
         "SAD is %d
",
         DEAD_STATE,
         ALIVE_STATE,
         HAPPY_STATE,
         SAD_STATE);
}

```

---

<div class="post-metadata">

### Author: ![MaxTheVool](https://avatars.discourse-cdn.com/v4/letter/m/9fc29f/32.png) [@MaxTheVool](https://boards.straightdope.com/u/MaxTheVool)
#### Post date: [June 1, 2005, 10:43pm UTC](https://boards.straightdope.com/t/another-tricky-c-preprocessor-question/306454/10 "2005-06-01T22:43:59Z")

</div>

> [@JohnM](#):
>
> And for your obfuscation pleasure, you can do it with a macro, just with the enum hidden behind the scenes:
> 
> ```auto
> 
> #include <stdlib.h>
> #include <stdio.h>
> 
> #define TABSTART enum{
> #define TABEND ENDTAB};
> #define PSEUDOCOUNTINGMACRO(v) v,
> 
> TABSTART
> PSEUDOCOUNTINGMACRO(DEAD_STATE)
> PSEUDOCOUNTINGMACRO(ALIVE_STATE)
> PSEUDOCOUNTINGMACRO(HAPPY_STATE)
> PSEUDOCOUNTINGMACRO(SAD_STATE)
> TABEND
> 
> void main(void)
> {
> printf("DEAD is %d
> "
> "ALIVE is %d
> "
> "HAPPY is %d
> "
> "SAD is %d
> ",
> DEAD_STATE,
> ALIVE_STATE,
> HAPPY_STATE,
> SAD_STATE);
> }
> 
> ```

Sadly, that won’t work for two reasons:  
(1) I’m not actually compiling C or C++ code, I’m just using the preprocessor. And enums are a feature of the language, not the preprocessor.

(2) More importantly, these macros need to be scattered throughout other things, not all sequential, ie:

```auto

COUNTINGMACRO(DEAD)
deadStateThing1
deadStateThing2

COUNTINGMACRO(ALIVE)
happyFunAliveThing(blah blah blah)

COUNTINGMACRO(HAPPY)

```

etc.

---

<div class="post-metadata">

### Author: ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)
#### Post date: [June 1, 2005, 10:55pm UTC](https://boards.straightdope.com/t/another-tricky-c-preprocessor-question/306454/11 "2005-06-01T22:55:32Z")

</div>

What exactly are you trying to do with this, anyway? There may be a more appropriate solution out there.

---

<div class="post-metadata">

### Author: ![MaxTheVool](https://avatars.discourse-cdn.com/v4/letter/m/9fc29f/32.png) [@MaxTheVool](https://boards.straightdope.com/u/MaxTheVool)
#### Post date: [June 1, 2005, 11:09pm UTC](https://boards.straightdope.com/t/another-tricky-c-preprocessor-question/306454/12 "2005-06-01T23:09:14Z")

</div>

> [@ultrafilter](#):
>
> What exactly are you trying to do with this, anyway? There may be a more appropriate solution out there.

As I said, I’ve written a scripting language for the game I’m working on, which is basically fully written and working. However, I’ve just gotten a request to add a new features which allow all the parts of a side mission to be encapsulated together.

Syntactically, I’d like the designers (who use the language) to do something like this:

```auto

BEGIN_SIDE_MISSION(happyMission)

SIDE_MISSION_STATE(TALK_TO_BOB)
//stuff in here relating to TALK_TO_BOB
SIDE_MISSION_STATE(DRINK_A_SODA)
//stuff in here relating to DRINK_A_SODE
END_SIDE_MISSION

```

It’s important that TALK\_TO\_BOB and DRINK\_A\_SODA be #defined, so that later on, language commands can do thinks like IF GETVAR(happyMission\_STATE) == DRINK\_A\_SODA. It’s also important that they be unique and, ideally, sequential.

The more of this work I can get the preprocessor to do for me, the happier I am. The simpler the syntax is (ie, the side missions states defined as they are used, not initially listed in some different place that can get out of sync), the happier the designers are.

---

<div class="post-metadata">

### Author: ![Polerius](https://avatars.discourse-cdn.com/v4/letter/p/d78d45/32.png) [@Polerius](https://boards.straightdope.com/u/Polerius)
#### Post date: [June 1, 2005, 11:26pm UTC](https://boards.straightdope.com/t/another-tricky-c-preprocessor-question/306454/13 "2005-06-01T23:26:29Z")

</div>

> [@MaxTheVool](#):
>
> Oh, and if it helps, I have control over how the preprocessor is called, since this is actually being used for a scripting language. So I could, potentially, call the preprocessor multiple times if that would help.
> 
> (I think I have a solution that would work if I ran the preprocessor twice and could figure out how to make a macro resolve with line feeds in it.)

Since you control how the pre-processor is called, why not something like this?

```auto

cpp input_file | perl script.pl | cpp > output_file 

```

The first cpp parses all the #includes and #defines  
The perl (or other language) script replaces COUNTINGMACRO(x) with #define x   
The second cpp takes care of all the #define x

If you don’t have #includes in your input file, you can simply use

```auto

perl script.pl input_file | cpp > output_file 

```

The perl script can be a simple one-liner run on the command line. e.g if the input\_file is

```auto

COUNTINGMACRO(DEAD_STATE)
COUNTINGMACRO(ALIVE_STATE)
COUNTINGMACRO(HAPPY_STATE)

int x1 DEAD_STATE;
int x2 ALIVE_STATE;
int x3 HAPPY_STATE;

```

Then

```auto

perl -ne 'if(/COUNTINGMACRO\(([A-Z_]+)\)/) {printf "#define $1 %i
", $counter++} else {print;}' input_file 

```

results in

```auto

#define DEAD_STATE 0
#define ALIVE_STATE 1
#define HAPPY_STATE 2

int x1 DEAD_STATE;
int x2 ALIVE_STATE;
int x3 HAPPY_STATE;

```

and the following

```auto

perl -ne 'if(/COUNTINGMACRO\(([A-Z_]+)\)/) {printf "#define $1 %i
", $counter++} else {print;}' input_file | cpp > output_file

```

results in

```auto

int x1 0 ;
int x2 1 ;
int x3 2 ;

```

---

<div class="post-metadata">

### Author: ![MaxTheVool](https://avatars.discourse-cdn.com/v4/letter/m/9fc29f/32.png) [@MaxTheVool](https://boards.straightdope.com/u/MaxTheVool)
#### Post date: [June 1, 2005, 11:33pm UTC](https://boards.straightdope.com/t/another-tricky-c-preprocessor-question/306454/14 "2005-06-01T23:33:17Z")

</div>

> [@Polerius](#):
>
> Since you control how the pre-processor is called, why not something like this?
> 
> ```auto
> 
> cpp input_file | perl script.pl | cpp > output_file 
> 
> ```

That’s actually almost precisely the approach I think I’m going to have to take… 3 passes. Once through the preprocessor to handle #include files and simple #defines, once through a custom-written preprocessor to expand “special” macros, and then again through the preprocessor to process any #defines generated by the special macros.

Of course, I don’t know Perl, so will have to just write the middle step preprocessor in C++ or something

---

<div class="post-metadata">

### Author: ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)
#### Post date: [June 1, 2005, 11:36pm UTC](https://boards.straightdope.com/t/another-tricky-c-preprocessor-question/306454/15 "2005-06-01T23:36:06Z")

</div>

> [@MaxTheVool](#):
>
> ```auto
> 
> BEGIN_SIDE_MISSION(happyMission)
> 
> SIDE_MISSION_STATE(TALK_TO_BOB)
> //stuff in here relating to TALK_TO_BOB
> SIDE_MISSION_STATE(DRINK_A_SODA)
> //stuff in here relating to DRINK_A_SODE
> END_SIDE_MISSION
> 
> ```
> 
> It’s important that TALK\_TO\_BOB and DRINK\_A\_SODA be #defined, so that later on, language commands can do thinks like IF GETVAR(happyMission\_STATE) == DRINK\_A\_SODA. It’s also important that they be unique and, ideally, sequential.

What about this?

```auto

#define SIDE_MISSION_STATE(x) static int x

```

The values of the various states wouldn’t necessarily be unique (in fact, they’d all be 0), but their addresses would be unique. They may or may not be sequential depending on your compiler.

---

<div class="post-metadata">

### Author: ![Polerius](https://avatars.discourse-cdn.com/v4/letter/p/d78d45/32.png) [@Polerius](https://boards.straightdope.com/u/Polerius)
#### Post date: [June 1, 2005, 11:38pm UTC](https://boards.straightdope.com/t/another-tricky-c-preprocessor-question/306454/16 "2005-06-01T23:38:10Z")

</div>

> [@MaxTheVool](#):
>
> Of course, I don’t know Perl, so will have to just write the middle step preprocessor in C++ or something

You can use the one-liner I showed above, replacing COUNTINGMACRO with whatever macro name you want to use.

Writing this in C++ would be a nightmare, I think.

---

<div class="post-metadata">

### Author: ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)
#### Post date: [June 1, 2005, 11:42pm UTC](https://boards.straightdope.com/t/another-tricky-c-preprocessor-question/306454/17 "2005-06-01T23:42:56Z")

</div>

Actually, if you’re really hung up on using the values, you can try this:

```auto

#define SIDE_MISSION_STATE(x) const static int x = (int) &x;

```

---

<div class="post-metadata">

### Author: ![MaxTheVool](https://avatars.discourse-cdn.com/v4/letter/m/9fc29f/32.png) [@MaxTheVool](https://boards.straightdope.com/u/MaxTheVool)
#### Post date: [June 2, 2005, 12:19am UTC](https://boards.straightdope.com/t/another-tricky-c-preprocessor-question/306454/18 "2005-06-02T00:19:44Z")

</div>

> [@ultrafilter](#):
>
> Actually, if you’re really hung up on using the values, you can try this:
> 
> ```auto
> 
> #define SIDE_MISSION_STATE(x) const static int x = (int) &x;
> 
> ```

Again, that’s a C++ thing, not a C++ _preprocessor_ thing.

There might be something similar and tricky that could be done with the **LINE** macro, but it wouldn’t end up generating things that were sequential.

---

<div class="post-metadata">

### Author: ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)
#### Post date: [June 2, 2005, 12:26am UTC](https://boards.straightdope.com/t/another-tricky-c-preprocessor-question/306454/19 "2005-06-02T00:26:52Z")

</div>

> [@MaxTheVool](#):
>
> Again, that’s a C++ thing, not a C++ _preprocessor_ thing.

I don’t understand the distinction you’re making. Why won’t that work?

> [@](#):
>
> There might be something similar and tricky that could be done with the **LINE** macro, but it wouldn’t end up generating things that were sequential.

If the statics are all in the same file and no variables are declared in between, your compiler will probably line them up sequentially. I’m 95% sure of that, but of course you’d want to check. In that case, they’d differ by sizeof(int) rather than 1, but you can correct for that easily.

---

<div class="post-metadata">

### Author: ![MaxTheVool](https://avatars.discourse-cdn.com/v4/letter/m/9fc29f/32.png) [@MaxTheVool](https://boards.straightdope.com/u/MaxTheVool)
#### Post date: [June 2, 2005, 1:18am UTC](https://boards.straightdope.com/t/another-tricky-c-preprocessor-question/306454/20 "2005-06-02T01:18:29Z")

</div>

> [@ultrafilter](#):
>
> I don’t understand the distinction you’re making. Why won’t that work?

Because the preprocessor won’t touch any of that at all. It will just leave it unchanged. The only things the preprocessor does (in general) are:  
-remove comments  
-expand macros  
-include files

My scripting language uses the preprocessor because all three of those things are generally useful for any language. But once the preprocessor has run, my compiler takes over, and C++ is out the window.

[Next page](https://boards.straightdope.com/t/another-tricky-c-preprocessor-question/306454.md?page=2)
