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.

Community
  • 1
  • 1
Lazer
  • 90,700
  • 113
  • 281
  • 364
  • 1
    i am curious to know what you need this for, maybe if we know we can find a better solution? – AndersK Sep 05 '10 at 13:35
  • @Anders K. I dont need it for anything as of now. I came across the linked SO thread and wondered if it was possible this way. As I suspected and as Tyler mentions, it is not possible. – Lazer Sep 05 '10 at 13:37
  • @Lazer as you yourself pointed out, best way is to use an array in that case. – AndersK Sep 05 '10 at 13:38
  • Application: [something like this](http://stackoverflow.com/questions/1132751/how-can-i-generate-unique-values-in-the-c-preprocessor), without having to do ugly preprocessor tricks. – Lazer Sep 05 '10 at 13:39
  • @Lazer- the only way to "dynamically" generate anything like this in C is by using the preprocessor. The compiler only interprets what is explicitly there. The feautre you are asking about is available in many interpreted languages, though (TCL, for example). – bta Sep 05 '10 at 13:49
  • Or by using some other tools. I used FreeMaker (http://freemarker.sourceforge.net/) for generating the java code. It is possible to use this framework for generating c++-code, what is probably desired. Than you can generate new variables before the compile time:) – LonliLokli Sep 05 '10 at 13:53
  • -1 for useless question asking how to do something useless. Use an array. – R.. GitHub STOP HELPING ICE Sep 05 '10 at 15:01

4 Answers4

8

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
  • 74,820
  • 18
  • 121
  • 166
  • +1 exactly what I was thinking but said much better than I could say it! –  Sep 05 '10 at 13:27
  • 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 Sep 05 '10 at 13:28
  • @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 Sep 05 '10 at 14:08
1

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).

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
1

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
  • 43,959
  • 6
  • 69
  • 99
  • +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. –  Sep 05 '10 at 13:50
0

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.