macro

N

nick4ng

I need to do something like this

#define P1(x) int x##__LINE__ = __LINE__;

int main(int argc, char* argv[])
{
P1(a);
P1(a);
}

I mean that the macro should be expanded in this way:

int main(int argc, char* argv[])
{
int a3 = 3;
int a4 = 4;
}


but the code above does not work.

How would you implement it?

Thanks
 
V

Victor Bazarov

I need to do something like this

#define P1(x) int x##__LINE__ = __LINE__;

int main(int argc, char* argv[])
{
P1(a);
P1(a);
}

I mean that the macro should be expanded in this way:

int main(int argc, char* argv[])
{
int a3 = 3;
int a4 = 4;
}


but the code above does not work.

How would you implement it?

You need a second level of indirection to use the ## :

#define TOGETHER(a,b) a##b
#define P1(x) int TOGETHER(x,__LINE__) = __LINE__

V
 
N

nick4ng

I need to do something like this
#define P1(x) int x##__LINE__ = __LINE__;
int main(int argc, char* argv[])
{
P1(a);
P1(a);
}
I mean that the macro should be expanded in this way:
int main(int argc, char* argv[])
{
int a3 = 3;
int a4 = 4;
}
but the code above does not work.
How would you implement it?

You need a second level of indirection to use the ## :

#define TOGETHER(a,b) a##b
#define P1(x) int TOGETHER(x,__LINE__) = __LINE__

V

I tried something like this (this is why my macro is called P1: there
was a P2). It looks like it does not work.
I am using VS 2005. The error message at compile time is: error C2374:
'a__LINE__': redefinition...

The code I run is:

#define TOGETHER(a,b) a##b
#define P1(x) int TOGETHER(x,__LINE__) = __LINE__;

int main(int argc, char* argv[])
{
P1(a);
P1(a);
}
 
V

Victor Bazarov

I need to do something like this
#define P1(x) int x##__LINE__ = __LINE__;
int main(int argc, char* argv[])
{
P1(a);
P1(a);
}
I mean that the macro should be expanded in this way:
int main(int argc, char* argv[])
{
int a3 = 3;
int a4 = 4;
}
but the code above does not work.
How would you implement it?

You need a second level of indirection to use the ## :

#define TOGETHER(a,b) a##b
#define P1(x) int TOGETHER(x,__LINE__) = __LINE__

V

I tried something like this (this is why my macro is called P1: there
was a P2). It looks like it does not work.
I am using VS 2005. The error message at compile time is: error C2374:
'a__LINE__': redefinition...

The code I run is:

#define TOGETHER(a,b) a##b
#define P1(x) int TOGETHER(x,__LINE__) = __LINE__;

int main(int argc, char* argv[])
{
P1(a);
P1(a);
}

Right. Sorry. Another level of indirection is needed:

#define TOGETHER(a,b) a ## b
#define TOGETHER2(a,b) TOGETHER(a,b)
#define P1(x) int TOGETHER2(x,__LINE__) = __LINE__
int main(int argc, char* argv[])
{
P1(a);
P1(a);
}

V
 
N

nick4ng

(e-mail address removed) wrote:
I need to do something like this
#define P1(x) int x##__LINE__ = __LINE__;
int main(int argc, char* argv[])
{
P1(a);
P1(a);
}
I mean that the macro should be expanded in this way:
int main(int argc, char* argv[])
{
int a3 = 3;
int a4 = 4;
}
but the code above does not work.
How would you implement it?
You need a second level of indirection to use the ## :
#define TOGETHER(a,b) a##b
#define P1(x) int TOGETHER(x,__LINE__) = __LINE__
V
I tried something like this (this is why my macro is called P1: there
was a P2). It looks like it does not work.
I am using VS 2005. The error message at compile time is: error C2374:
'a__LINE__': redefinition...
The code I run is:
#define TOGETHER(a,b) a##b
#define P1(x) int TOGETHER(x,__LINE__) = __LINE__;
int main(int argc, char* argv[])
{
P1(a);
P1(a);
}

Right. Sorry. Another level of indirection is needed:

#define TOGETHER(a,b) a ## b
#define TOGETHER2(a,b) TOGETHER(a,b)
#define P1(x) int TOGETHER2(x,__LINE__) = __LINE__
int main(int argc, char* argv[])
{
P1(a);
P1(a);
}

V

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,163
Messages
2,570,898
Members
47,438
Latest member
arden

Latest Threads

Top