tags:

views:

142

answers:

4

With reference to the SO thread C Macro Token Concatenation involving a variable - is it possible?,

Is it at all possible to generate variable names at compile-time in C and C++?

something like

int count = 8;
for(i=0; i<count; i++) {
    int var_%i% = i*i;   // <--- magic here
}

I know I can use arrays for this case, but this is just an example just to explain what I mean.

+7  A: 

If you are expecting to use the value of i to generate the name var_%i% (e.g. generating variables var_1, var_2, ..., var_count), then no, that's not possible at all. For one thing, that's not even a compile-time operation. The value of i isn't known until runtime. Yes, you can tell what it will be (and maybe a compiler could with static analysis in a very simple case), but in general values are exclusively run-time concepts.

If you just mean creating a variable called var_i, why don't you just name it that?

Maybe it would help if you explained what problem you're trying to solve by doing this. I guarantee there's a better way to go about it.

Tyler McHenry
+1 exactly what I was thinking but said much better than I could say it!
Ninefingers
@Tyler: I have updated the example to make it full compiletime possible. For this example that I came up with, I know arrays are a better solution. I am wondering if it is possible to do something like what I have shown in the question, using normal variables.
Lazer
@Lazer It's still not *in general* possible to do this at compile time, which is why C does not support it. If this were to be a C language feature, it would have to be possible *in general*, not just in a few specific simplified cases. Plus, even in your specific case, it requires static analysis, which is not something that C compilers are required to do.
Tyler McHenry
+1 for last paragraph/sentence.
R..
+1  A: 

In C++, you can achieve things a bit like this with templates (but I'm no expert, so I'll say no more). Google for "template metaprogramming". However, this isn't based on variables (in the run-time sense).

In C, this cannot be done (well, certainly nothing close to your example).

Oli Charlesworth
+1  A: 

You can use macros to build variable names, but I have yet to find a case where it's a good idea to do so. It cannot be done as in your example, since i does not have a value that the pre-processor can interpret. You can only build variable names using things that have been explicitly #defined, so the usefulness of "dynamic" variable names is quite limited.

bta
+1, I agree - macros are for making code slightly more readable and not for replacing parts of the language or creating the illusion of object-orientated or some other thing like that.
Ninefingers
A: 

Firstly, it can't be done, secondly, why would you want to? Remember that loop will be seen by a compiler and evaluated to some assembly code:

    mov rcx, 0
_loop_label:
    add rcx, 1
    ; do some stuff

    cmp rcx, count
    jl _loop_label

    ; continue program

Then array access looks something like this (which is how pointer arithmetic works):

mov rbx, [baseaddress+rcx*4]

Disclaimer: your compiler definitely writes better assembly than I do.

So, when you look at this, all you're doing is accessing a memory address offset by the iteration number multiplied by the size of the data type in question. What use is it to give each of these a unique, defined name inside your executable when you have enough information to get to the memory address in question in any case? You certainly won't actually find the variable name inside the resultant assembly.

I suspect that if what you were trying to do was possible, the compiler would simply optimise it out.

Ninefingers