remove server silliniess
Martin Fuchs
Martin-Fuchs at gmx.net
Fri Apr 18 07:27:24 CDT 2003
> Well, there seem to be some reasonable tricks for that:
> http://www.jaggersoft.com/pubs/CVu11_3.html
> http://www.panelsoft.com/murphyslaw/apr01.htm
>
> This is for C++ but maybe also adaptable for C:
> http://www.boost.org/libs/static_assert/static_assert.htm
Here is my version, which has some advantages over the mentioned compile time asserts.
It uses the constraint, that a typedef'd array can't have zero length.
-> It doesn't generate any code in the compiled binary. (no use of switch or other constructs)
-> It doesn't use any memory in the running program. (no declaration of variables)
It generates a unique name for the typedef.
-> You can use any number of assertions, if you put any other in its own source code line.
You don't have to put the assert into a function. You can put it anywhere at global scope
#define MAKE_UNIQUE_(x,y) __unique_name_##x##y
#define MAKE_UNIQUE(x,y) MAKE_UNIQUE_(x,y)
#define UNIQUE_NAME(name) MAKE_UNIQUE(name, __LINE__)
/* compile time assertions; An assertion failure results in error C2466: cannot allocate an array of constant size 0 */
#define CASSERT(expr) typedef char UNIQUE_NAME(cassert_type)[(expr)? 1: 0];
/* Examples */
CASSERT(sizeof(char)>=1);
CASSERT(sizeof(int)>=2);
CASSERT(sizeof(long)=4);
CASSERT(sizeof(__int64)=8);
Using the CASSERT macro you can define some more convenient macros for checking type sizes:
#include <limits.h>
#define ASSERT_MIN_BITSIZE(type, size) CASSERT(sizeof(type)*CHAR_BIT >#define ASSERT_EXACT_BITSIZE(type, size) CASSERT(sizeof(type)*CHAR_BIT = (size));
/* Examples */
ASSERT_MIN_BITSIZE(char, 8);
ASSERT_MIN_BITSIZE(int, 16);
ASSERT_EXACT_BITSIZE(long, 32);
ASSERT_EXACT_BITSIZE(__int64, 64);
--
Martin Fuchs
martin-fuchs at gmx.net
More information about the wine-devel
mailing list