我想声明一个根据传递给宏的参数定义的 C 宏。换句话说,我想#if
在 C 宏中使用指令。它应该是这样的:
#define XLType_id 1
#define XLTypeCastToVoidPointer(type) #if XLType_##type \\
(__bridge void *)\\
#define XLTypeCast(type) (__bridge id)\\
#else \\
&\\
#define XLTypeCast(type) *(type*)\\
#endif
我想使用XLTypeCastToVoidPointer()
andXLTypeCast()
宏来重新转换(void *)
类型的变量。
- (NSInteger) value{
NSInteger returnValue = 100;
// the same as -> void *aValueToReturn = &returnValue;
void *aValueToReturn = XLTypeCastToVoidPointer(NSInteger) returnValue;
// Do something with this void pointer here.
// the same as -> return *(NSInteger*) aValueToReturn;
return XLTypeCast(NSInteger) aValueToReturn;
}
- (NSString*)string{
NSString* aString = @"a string";
// the same as -> void *aValueToReturn = &returnValue;
void *aValueToReturn = XLTypeCastToVoidPointer(id) aString;
// Do something with this void pointer here.
// the same as -> return *(NSInteger*) aValueToReturn;
return XLTypeCast(id) aString;
}
但是,似乎不允许#if
在 C 宏中使用指令。有没有办法做到这一点?