请教C++的类型转换的问题
c++我不太熟,用c语言编译没问题,但c++不行。大概是这样的,我需要把一个整数转成指针,然后再转成整数,带式类似于
程序代码:
#define U2P(u) ((void*)(unsigned long)(u)) #define P2U(p) ((unsigned long)(void*)(p)) #define A U2P(2) int foo(void *p) { switch(P2U(p)) { case P2U(A): return 1; default: return 0; } }
但是编译出现
$ g++ -c a.cc
a.cc: In function 'int foo(void*)':
a.cc:1:42: error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
#define U2P(u) ((void*)(unsigned long)(u))
^
a.cc:2:41: note: in definition of macro 'P2U'
#define P2U(p) ((unsigned long)(void*)(p))
^
a.cc:4:13: note: in expansion of macro 'U2P'
#define A U2P(2)
^
a.cc:9:11: note: in expansion of macro 'A'
case P2U(A): return 1;
^
a.cc:2:42: error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
#define P2U(p) ((unsigned long)(void*)(p))
^
a.cc:9:7: note: in expansion of macro 'P2U'
case P2U(A): return 1;
^
a.cc: In function 'int foo(void*)':
a.cc:1:42: error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
#define U2P(u) ((void*)(unsigned long)(u))
^
a.cc:2:41: note: in definition of macro 'P2U'
#define P2U(p) ((unsigned long)(void*)(p))
^
a.cc:4:13: note: in expansion of macro 'U2P'
#define A U2P(2)
^
a.cc:9:11: note: in expansion of macro 'A'
case P2U(A): return 1;
^
a.cc:2:42: error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
#define P2U(p) ((unsigned long)(void*)(p))
^
a.cc:9:7: note: in expansion of macro 'P2U'
case P2U(A): return 1;
^
我应该怎么改才是正确的?