typedefwas a signed or unsigned integral type (e.g.
ssize_t, etc.) in C. In C++ it would be easy with a little bit of meta-programing but in C there are far fewer options when it comes to static (aka compile-time) checking. And on this one, Google didn't help me much. So I thought I'd write something, hopefully it'll be helpful to someone searching what I was trying to find.
Here is a simple trick:
#define IS_UNSIGNED_TYPE(Type) \
char ERROR_ ## Type ## _MUST_BE_UNSIGNED[((Type) -1 < 0) * -1]
Now if you call
IS_UNSIGNED_TYPE(ssize_t);GCC will give you a nice
error: size of array 'ERROR_ssize_t_MUST_BE_UNSIGNED' is negative, whereas with
size_tit compiles fine.
For those who don't understand how this works, it's pretty simple: the two hashes (
##) are used to concatenate tokens in the macro. So for instance for the case of
char ERROR_ssize_t_MUST_BE_UNSIGNED[((ssize_t) -1 < 0) * -1]Whereas for
char ERROR_ssize_t_MUST_BE_UNSIGNED[(-2147483648 < 0) * -1]
char ERROR_ssize_t_MUST_BE_UNSIGNED[1 * -1]
char ERROR_ssize_t_MUST_BE_UNSIGNED[-1] // Invalid
char ERROR_size_t_MUST_BE_UNSIGNED[((size_t) -1 < 0) * -1]Not particularly useful but ... Kind of neat, isn't it?
char ERROR_size_t_MUST_BE_UNSIGNED[(4294967295 < 0) * -1]
char ERROR_size_t_MUST_BE_UNSIGNED[0 * -1]
char ERROR_size_t_MUST_BE_UNSIGNED // Valid
如果我將此 char 的值 與 0xffffff 作 OR 運算，然後偵測是否小於 0 ，不就可知道此 char 是否為 signed 或是 unsigned 了嗎？
So If I translate the previous comment via Google's language tools, it (approximately) reads:
"If I were to char and the value of the OR for 0xffffff computing, and then detect whether or not less than 0, can not know whether this char for unsigned or signed it?
Xiangliaoyixiang 1:00 am sorry, did not write a small program Test"
To which I'd answer that I don't see any benefit especially since 0xffffff is a fixed value and it's not suitable for all numeric data type (some are shorter, some are larger).
Nice brief and this mail helped me alot in my college assignement. Say thank you you as your information.
Thanks for taking the time to find the answer and publish it.. :)
Post a Comment