Sign in to follow this  
peter_b

Compile-time indexing magic?

Recommended Posts

Is there any nifty preprocessor trick to generate an increasing index at compiletime? For instance, i want to do something like this: int one = AUTO_MAGIC_INDEX; int two = AUTO_MAGIC_INDEX; int three = AUTO_MAGIC_INDEX; int four = AUTO_MAGIC_INDEX; which would generate: one == 1; two == 2; three == 3; four == 4; What i want to use this for is to be able to have a class-member index.

Share this post


Link to post
Share on other sites
In using GCC 4.1 im afraid, so it's not supported. Changing compiler is not an option either.

Is there any other way to simulate this behaviour with some macro tricks or something?

Share this post


Link to post
Share on other sites
That's what his code is doing. Here's an equivalent example:
enum { blue = 1, red, green, yellow };

In this example, yellow has the value 4.

Share this post


Link to post
Share on other sites
Quote:
Original post by peter_b
Is there any other way to simulate this behaviour with some macro tricks or something?
Not that I'm aware of - this is why __COUNTER__ was invented.

Share this post


Link to post
Share on other sites
Quote:
it has to automatically increase an assignable value


This is what enum does.

Otherwise:
int getNextIndex() {
static count;
return count++;
};



There is another option involving template meta magic, but it's hideously complex.

Share this post


Link to post
Share on other sites
Yeah but the point is automatic generation of indexes.

Lets say we have a class:

template<int IDX>
class IndexedMember
{
enum {m_index = IDX};
};


Then it should be possible to use it like this:

class Whatever
{
IndexedMember<__COUNTER_EQUALENT__> m1;
IndexedMember<__COUNTER_EQUALENT__> m2;
IndexedMember<__COUNTER_EQUALENT__> m3;
}

I dont want to do:

class Whatever
{
IndexedMember<yellow> m1;
IndexedMember<red> m2;
IndexedMember<blue> m3;
}

because then i might aswell do:

class Whatever
{
IndexedMember<0> m1;
IndexedMember<1> m2;
IndexedMember<2> m3;
}

Which is the way it works right now, which is really bad since the project is huge and it causes many problems when new members are added and the updating of indices are overlooked.

Share this post


Link to post
Share on other sites
If you are on Unix, you can do so:

int main () {
using std::cout;

int one =
#include</dev/tty>
;
int two =
#include</dev/tty>
;
int three =
#include</dev/tty>
;
int four =
#include</dev/tty>
;
}


because in your initial post, you did not mention that the generation be automatic. On the other, can't test atm, you could maybe establish a pipe into which is pumped a sequence, one by one.

Interesting problem. Maybe I will have a better idea sooner or later.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this