M
Mark
Hello
have found a nice C-language trick on a Web. It implements a compile time
'asserts':
#define COMPILE_TIME_ASSERT(cond, msg) char msg[(cond) ? 1 : -1]
int main(void)
{
int p = 1;
COMPILE_TIME_ASSERT(p > 2, data);
return 0;
}
Unfortunately, it doesn't act as I thought it would. I expected a compiler
(gcc 4.1.1) emits error or at least warning about negative array index, but
it didn't (in both C89 and C99 modes). But it doesn complain if I explicitly
declare an array with negative index:
int k[-1]; /* error: size of array 'k' is negative */
Is the compile-time assertion method not portable ?
have found a nice C-language trick on a Web. It implements a compile time
'asserts':
#define COMPILE_TIME_ASSERT(cond, msg) char msg[(cond) ? 1 : -1]
int main(void)
{
int p = 1;
COMPILE_TIME_ASSERT(p > 2, data);
return 0;
}
Unfortunately, it doesn't act as I thought it would. I expected a compiler
(gcc 4.1.1) emits error or at least warning about negative array index, but
it didn't (in both C89 and C99 modes). But it doesn complain if I explicitly
declare an array with negative index:
int k[-1]; /* error: size of array 'k' is negative */
Is the compile-time assertion method not portable ?