# Tricky C/C++ Preprocessor Question

**URL:** <https://boards.straightdope.com/t/tricky-c-c-preprocessor-question/265953>\
**Category:** Factual Questions\
**Created:** [September 24, 2004, 10:26pm UTC](https://boards.straightdope.com/t/tricky-c-c-preprocessor-question/265953 "2004-09-24T22:26:03Z")\
**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:** [September 24, 2004, 10:26pm UTC](https://boards.straightdope.com/t/tricky-c-c-preprocessor-question/265953/1 "2004-09-24T22:26:03Z")

</div>

OK, I’m trying to do some tricky preprocessor stuff, and I’ve gotten a bit stuck.

Basically, I want to be able to have something like this at the top of a C file:

TEST(10,20)  
TEST(30,40)  
TEST(50,60)

which expands to  
testStruct unique1(10,20);  
testStruct unique2(30,40);  
testStruct unique3(50,60);  
basically, each time the macro is evaluated, I need it to generate a unique identifier that is part of its expansion.

I tried to do that using the **LINE** macro, something like this:

#define TEST(a,b) testStruct unique## **LINE** (a,b);

the trouble is, the ## gets evaluated first, resulting in unique\_\_LINE\_\_, and then I end up with multiple testStructs called unique\_\_LINE\_\_.  
Does anyone have any crafty suggestions as to how to get around this? Or an alternate strategy that would accomplish what I’m trying to accomplish?

---

<div class="post-metadata">

**Author:** ![iamthewalrus\_3](https://avatars.discourse-cdn.com/v4/letter/i/258eb7/32.png) [@iamthewalrus\_3](https://boards.straightdope.com/u/iamthewalrus_3)\
**Post date:** [September 24, 2004, 10:58pm UTC](https://boards.straightdope.com/t/tricky-c-c-preprocessor-question/265953/2 "2004-09-24T22:58:50Z")

</div>

I’m still looking for preprocessor ways to make unique stuff, but why not make a big (enough) array of said structs, and expand out to something like:

testArray[**LINE** - STARTING\_LINE] = testStruct(x, y);

---

<div class="post-metadata">

**Author:** ![z\_z\_z](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@z\_z\_z](https://boards.straightdope.com/u/z_z_z)\
**Post date:** [September 24, 2004, 11:15pm UTC](https://boards.straightdope.com/t/tricky-c-c-preprocessor-question/265953/3 "2004-09-24T23:15:23Z")

</div>

> [@MaxTheVool](#):
>
> OK, I’m trying to do some tricky preprocessor stuff, and I’ve gotten a bit stuck.  
> \<snip…\>
> 
> basically, each time the macro is evaluated, I need it to generate a unique identifier that is part of its expansion.

Sorry, I have no real solution for your problem. Just a suggestion: You could make you macro call a function that includes that information, or even a macro that substitutes for another macro that calls a function. Of course, if you are after a unique identifier after the preprocessor pass, my “solution” doesn´t work.  
What I´m suggesting is:

#define TEST(a,b) test( **FILE** , **LINE** , a, b)

and then have some (global) function test() that does what you want.

or

#define TEST1(a, b) TEST2( **FILE** , **LINE** , a, b)  
#define TEST2(f, l, a, b) test (f, l, a, b)  
and so on.

That only affects the arguments to your call. If want to create an unique token after the preprocessor pass, I´m not sure what you could do. I think I remenber some usage of /\* and \*/ to concatenate _SOMETHING_ in the preprocessor, but I would have to look that up.

---

<div class="post-metadata">

**Author:** ![John\_T.Conklin](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/john_t.conklin/32/3307_2.png) [@John\_T.Conklin](https://boards.straightdope.com/u/John_T.Conklin)\
**Post date:** [September 24, 2004, 11:54pm UTC](https://boards.straightdope.com/t/tricky-c-c-preprocessor-question/265953/4 "2004-09-24T23:54:28Z")

</div>

> [@MaxTheVool](#):
>
> Does anyone have any crafty suggestions as to how to get around this? Or an alternate strategy that would accomplish what I’m trying to accomplish?

**z\_z\_z** ’s suggestion to use two macros almost works. A third puts it over the top.

```auto

#define CONCAT(a,b) a ## b
#define TEST2(a,b,c) testStruct CONCAT(unique,c)(a,b);
#define TEST(a,b) TEST2(a,b, __LINE__ )

```

However, I’m having a hard time thinking of of a situation where this would be useful. Perhaps you could share what problem you’re trying to solve with the uniquely-named identifiers and we could come up with a even better solution.

---

<div class="post-metadata">

**Author:** ![z\_z\_z](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@z\_z\_z](https://boards.straightdope.com/u/z_z_z)\
**Post date:** [September 25, 2004, 12:09am UTC](https://boards.straightdope.com/t/tricky-c-c-preprocessor-question/265953/5 "2004-09-25T00:09:00Z")

</div>

A quick followup…

I reread your OP and wonder - what are you excactly trying to accomplish?

Are you after a unique token after the preprocessor pass? Or just some way to distiguish between different “calls” to your macro, even if the arguments are the same?

I´m just guessing, but if it is the latter, and you want to differentiate between

\<line 123\> TEST(20, 20)  
and  
\<line 234\> TEST(20, 20)

and have some sort of sequential numbering, then you could try the following

#define TEST(a, b) test(get\_num(), a, b)

and have a array of test\_struct[MAX]

and reference test\_struct\* in test(i, a, b) and of course a global function get\_num() that returns sequential numbers 0…MAX - 1.

Sorry if this is not what you´re after, but tell us more…

And yes, ## is what I had in mind, instead of /\* and \*/. I can´t look it up now, but does´t this also have some use in the preprocessor, or did I cofuse the two?

---

<div class="post-metadata">

**Author:** ![Ms2001](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ms2001/32/18463_2.png) [@Ms2001](https://boards.straightdope.com/u/Ms2001)\
**Post date:** [September 25, 2004, 1:04am UTC](https://boards.straightdope.com/t/tricky-c-c-preprocessor-question/265953/6 "2004-09-25T01:04:32Z")

</div>

> [@z\_z\_z](#):
>
> And yes, ## is what I had in mind, instead of /\* and \*/. I can´t look it up now, but does´t this also have some use in the preprocessor, or did I cofuse the two?

Before a##b was widely supported, people used to use a/\*\*/b to concatenate, trusting the preprocessor to remove comments after replacing macro arguments without leaving a space. That’s deprecated now.

---

<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:** [September 25, 2004, 2:08am UTC](https://boards.straightdope.com/t/tricky-c-c-preprocessor-question/265953/7 "2004-09-25T02:08:13Z")

</div>

If you want a picture of what’s going on in time-sensitive code, or an environment with a lot of threads, something like this can be handy, as you can pass off all the output to another program. My employer has something like this, but we have a whole system set up to generate unique IDs.

**z\_z\_z** ’s solution is probably about as good as you’re going to get without something like that, but it seems like it would only work for a single file–if two TEST statements are on the same line in different files, you wouldn’t be able to distinguish them.

---

<div class="post-metadata">

**Author:** ![Ms2001](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ms2001/32/18463_2.png) [@Ms2001](https://boards.straightdope.com/u/Ms2001)\
**Post date:** [September 25, 2004, 2:57am UTC](https://boards.straightdope.com/t/tricky-c-c-preprocessor-question/265953/8 "2004-09-25T02:57:50Z")

</div>

> [@ultrafilter](#):
>
> **z\_z\_z** ’s solution is probably about as good as you’re going to get without something like that, but it seems like it would only work for a single file–if two TEST statements are on the same line in different files, you wouldn’t be able to distinguish them.

**FILE** returns the current source file’s name as a string. You can’t put it in an identifier, but you can pass it to the constructor; collisions will only be a problem if the identifiers are global.

---

<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:** [September 25, 2004, 3:05am UTC](https://boards.straightdope.com/t/tricky-c-c-preprocessor-question/265953/9 "2004-09-25T03:05:18Z")

</div>

> [@Mr2001](#):
>
> **FILE** returns the current source file’s name as a string. You can’t put it in an identifier, but you can pass it to the constructor; collisions will only be a problem if the identifiers are global.

I could’ve sworn that we used that as part of a macro at work, as a parameter to a printf statement. It looks like I’ll have an opportunity to check it out tomorrow. :mad:

There might also be a way to do this with templates, but there are a couple issues there:

1. It’ll slow your compilation to a crawl. That might not be really bad, but it is something to consider.

2. No compiler handles templates correctly, and no two compilers mishandle them in exactly the same way. If this for only one compiler, that might not be an issue.

---

<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:** [September 25, 2004, 3:10am UTC](https://boards.straightdope.com/t/tricky-c-c-preprocessor-question/265953/10 "2004-09-25T03:10:33Z")

</div>

> [@ultrafilter](#):
>
> There might also be a way to do this with templates, but there are a couple issues there:
> 
> 1. It’ll slow your compilation to a crawl. That might not be really bad, but it is something to consider.
> 
> 2. No compiler handles templates correctly, and no two compilers mishandle them in exactly the same way. If this for only one compiler, that might not be an issue.

Never mind. To get a unique ID from template code, you need to pass a unique ID in.

---

<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:** [September 25, 2004, 3:41am UTC](https://boards.straightdope.com/t/tricky-c-c-preprocessor-question/265953/11 "2004-09-25T03:41:05Z")

</div>

> [@MaxTheVool](#):
>
> basically, each time the macro is evaluated, I need it to generate a unique identifier that is part of its expansion.

The more I think about this, the more confused I get. What are you trying to identify here? Do you just want to create a new object of type testStruct?

---

<div class="post-metadata">

**Author:** ![Ms2001](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ms2001/32/18463_2.png) [@Ms2001](https://boards.straightdope.com/u/Ms2001)\
**Post date:** [September 25, 2004, 4:11am UTC](https://boards.straightdope.com/t/tricky-c-c-preprocessor-question/265953/12 "2004-09-25T04:11:57Z")

</div>

> [@ultrafilter](#):
>
> What are you trying to identify here? Do you just want to create a new object of type testStruct?

That looks like it from the desired sample code in the OP.

Since there’s no way to refer to these objects (without knowing which line they were created on, which will change constantly as you add code), and since a constructor call for an object you’ll never use is just a function call with a memory leak (temporary, if it’s on the stack), I can only assume the point is to do something when the object is automatically destructed - write a log message, release a lock that was acquired in the constructor, 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:** [September 25, 2004, 3:49pm UTC](https://boards.straightdope.com/t/tricky-c-c-preprocessor-question/265953/13 "2004-09-25T15:49:27Z")

</div>

> [@Mr2001](#):
>
> That looks like it from the desired sample code in the OP.
> 
> Since there’s no way to refer to these objects (without knowing which line they were created on, which will change constantly as you add code), and since a constructor call for an object you’ll never use is just a function call with a memory leak (temporary, if it’s on the stack), I can only assume the point is to do something when the object is automatically destructed - write a log message, release a lock that was acquired in the constructor, etc.

I guess that makes sense.

I’d do it like this:

```auto

#define UNIQUE_ID ( __LINE__ << 24) + (atoi( __TIME__ ) << 16) + (atoi(& __TIME__ [3]) << 8) + atoi(& __TIME__ [6])

#define TEST(a, b) testStruct<UNIQUE_ID>(a, b)

template <int n> struct testStruct
{
    ....put whatever you need in here
};

```

Now, that does have the problem that the value of UNIQUE\_ID will change whenever you add or delete code, and whenever you recompile. But if you don’t care about that, it’s not a problem.

---

<div class="post-metadata">

**Author:** ![CaveMike](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/cavemike/32/16379_2.png) [@CaveMike](https://boards.straightdope.com/u/CaveMike)\
**Post date:** [September 26, 2004, 5:25am UTC](https://boards.straightdope.com/t/tricky-c-c-preprocessor-question/265953/14 "2004-09-26T05:25:16Z")

</div>

**ultrafilter** ’s template example should work. Here is a [CodeProject](http://codeproject.com/win32/callback_adapter.asp?msg=388581&mode=all&userid=55318#xx388581xx) article that uses the same technique (scroll to the paragraph just before _Conclusion_). The nice thing about this article is that he walks the logic up to the final solution.

---

<div class="post-metadata">

**Author:** ![Shalmanese](https://avatars.discourse-cdn.com/v4/letter/s/45deac/32.png) [@Shalmanese](https://boards.straightdope.com/u/Shalmanese)\
**Post date:** [September 26, 2004, 3:06pm UTC](https://boards.straightdope.com/t/tricky-c-c-preprocessor-question/265953/15 "2004-09-26T15:06:02Z")

</div>

Instead of trying to do it in the preprocessor, I would make a simple perl script and integrate that into a makefile which gives you much more flexibility and the removal of nasty side-effects that macros can sometimes lend to the code.

---

<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:** [September 26, 2004, 3:31pm UTC](https://boards.straightdope.com/t/tricky-c-c-preprocessor-question/265953/16 "2004-09-26T15:31:20Z")

</div>

I dunno, I think we still need to know why the OP wants to do this to provide a meaningful solution.

---

<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:** [September 27, 2004, 3:25am UTC](https://boards.straightdope.com/t/tricky-c-c-preprocessor-question/265953/17 "2004-09-27T03:25:35Z")

</div>

Thanks for all the advice and answers… one of my coworkers came up with a 3-macro solution similar to the one proposed by **John T. Conklin** , although I would have to go reread the K&R chapter about the preprocessor to really be certain I understood what was going on.  
As for what it’s for, **Mr2001** ’s guess is almost right… I’m using a dummy class which takes up no space, but whose constructor function contains snippets of code that I want to make sure run before anything else (well, anything not in a constructor function for a static object) runs. For instance, I might want to register various static variables of various classes with some central debug system. So, I’d have a function like DebugSystem()::RegisterVariable(void \*pVar, enumVarType eType, char \*pName), and I might have variables to register scattered around between different files and modules, Render, Events, Physics, Collision. So obviously I could just add calls to DebugSystem()::RegisterVariable() into init functions for each of these modules, but that assumes there ARE init functions for each of these modules, that they’re all called before anything important happens, etc.

Using cool macros of this sort, I can just stick a REGISTER\_VARIABLE macro at file scope, and I know that the code will get called once and only once at startup time.  
(It wouldn’t surprise me if there were a more efficient way to do this… but my C++ knowledge was acquired in a somewhat ad hoc fashion.)

---

<div class="post-metadata">

**Author:** ![z\_z\_z](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@z\_z\_z](https://boards.straightdope.com/u/z_z_z)\
**Post date:** [September 27, 2004, 6:27pm UTC](https://boards.straightdope.com/t/tricky-c-c-preprocessor-question/265953/18 "2004-09-27T18:27:09Z")

</div>

> [@MaxTheVool](#):
>
> \<snip\>
> 
> For instance, I might want to register various static variables of various classes with some central debug system. So, I’d have a function like DebugSystem()::RegisterVariable(void \*pVar, enumVarType eType, char \*pName), and I might have variables to register scattered around between different files and modules, Render, Events, Physics, Collision. So obviously I could just add calls to DebugSystem()::RegisterVariable() into init functions for each of these modules, but that assumes there ARE init functions for each of these modules, that they’re all called before anything important happens, etc.
> 
> Using cool macros of this sort, I can just stick a REGISTER\_VARIABLE macro at file scope, and I know that the code will get called once and only once at startup time.

Wow. Sorry, i don´t want to shout, but DON´T DO THIS!!!  
That´s not how macros work. The preprocessor is simply a (parametric) text replacement tool that runs before the code is actually compiled. NO functions/methods/constructors or whatever will be called and no code will be excuted by the text replacement.

If you have a macro

#define TEST(a) exit(a)

then the preprocessor simply replaces each occurence of TEST(a) with exit(a), with “a” being a parameter. No code will be excuted. Then the resulting programm is compiled. Think of it like having a sed or perl script in your makefile and passing the output of this script to the compiler.

---

<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:** [September 27, 2004, 6:32pm UTC](https://boards.straightdope.com/t/tricky-c-c-preprocessor-question/265953/19 "2004-09-27T18:32:29Z")

</div>

> [@MaxTheVool](#):
>
> Using cool macros of this sort, I can just stick a REGISTER\_VARIABLE macro at file scope, and I know that the code will get called once and only once at startup time.

Like **z\_z\_z** said, this doesn’t do what you want it to. If you’re looking for something like this, the following class might be helpful:

```auto

class ExecuteOnce
{
    public:
        ExecuteOnce();
        ~ExecuteOnce();

    private:
        void Execute();

        static bool Executed;
};

bool ExecuteOnce::Executed = false;

ExecuteOnce::ExecuteOnce()
{
    if ( !Executed )
    {
        Execute();
        Executed = true;
    }
}

ExecuteOnce::~ExecuteOnce()
{
}

void ExecuteOnce::Execute()
{
    // Do whatever you need here
}

```

---

<div class="post-metadata">

**Author:** ![Omphaloskeptic](https://avatars.discourse-cdn.com/v4/letter/o/bcef8e/32.png) [@Omphaloskeptic](https://boards.straightdope.com/u/Omphaloskeptic)\
**Post date:** [September 27, 2004, 8:36pm UTC](https://boards.straightdope.com/t/tricky-c-c-preprocessor-question/265953/20 "2004-09-27T20:36:05Z")

</div>

> [@z\_z\_z](#):
>
> Wow. Sorry, i don´t want to shout, but DON´T DO THIS!!!  
> That´s not how macros work. The preprocessor is simply a (parametric) text replacement tool that runs before the code is actually compiled. NO functions/methods/constructors or whatever will be called and no code will be excuted by the text replacement.
> 
> If you have a macro
> 
> #define TEST(a) exit(a)
> 
> then the preprocessor simply replaces each occurence of TEST(a) with exit(a), with “a” being a parameter. No code will be excuted. Then the resulting programm is compiled. Think of it like having a sed or perl script in your makefile and passing the output of this script to the compiler.

I think he’s suggesting that his macros are being used at file scope and expand into object definitions. The constructors for such objects are called before the execution of the main program, which seems to be what he wants. E.g., if I have a macro

#define TEST(a,b) TestObj a(b)

(where TestObj is some class with constructor TestObj(int) ) and I have

TEST(x,3); // expands to TestObj x(3);  
int main( ) { TestObj(y,4); }

at file scope, then the constructor TestObj(3) will be called before TestObj(4). The macros are simply a way to generate the appropriate inputs to the constructor from the preprocessor’s **LINE** macro.

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