Meta assert
by Jere Sanisalo · in Torque Game Engine · 02/24/2002 (3:14 am) · 0 replies
Here is a small code snippet for the intrested. It's called MetaAssert, and it works like a normal assert, except it is evaluated at compile time (and effectively adds NO code to the compiled output).
The main use of MetaAssert is in places and situation which should not compile, eg. some constant out of range or invalid template parameters or whatever.
The main limitation for MetaAssert is that it MUST evaluate to a constant (which will be evaluated to true or false).
I use MetaAssert in my own projects to mainly guarantee that certain template parameters are correct and at platform level to guarantee that eg. S32 is actually 4 bytes wide.
Add the following to platform/platformAssert.h:
Hope this helps someone (and gets into the main source tree ;).
The main use of MetaAssert is in places and situation which should not compile, eg. some constant out of range or invalid template parameters or whatever.
The main limitation for MetaAssert is that it MUST evaluate to a constant (which will be evaluated to true or false).
I use MetaAssert in my own projects to mainly guarantee that certain template parameters are correct and at platform level to guarantee that eg. S32 is actually 4 bytes wide.
Add the following to platform/platformAssert.h:
// Meta assertion #define MetaAssert( expr ) \ typedef char _MetaAssert_##__LINE__[ ( expr ) ? 1 : -1 ];
Hope this helps someone (and gets into the main source tree ;).