DevMaster.net Forums
[[ Home | Forums | 3D Engines Database | Wiki | Articles/Tutorials | Game Dev Jobs | IRC Chat Network | Contact Us ]]

Go Back   DevMaster.net Forums > Site Discussions > Code & Snapshot Discussion
User Name
Password
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Display Modes
Old 09-08-2004, 11:26 AM   #1
john
Member
 
Join Date: Jan 2003
Posts: 85
Default

Have you ever wanted to do compile-time checks on constant-expressions? Well, one way is to use the known method assert(expr). However, this type of check is performed at run-time. Sometimes, compile-time checks is needed, which is what this code will do:

Code:
// C_ASSERT() can be used to perform many compile-time assertions: // type sizes, field offsets, etc. // An assertion failure results in error C2118: negative subscript. #define C_ASSERT(e) typedef char __C_ASSERT__[(e)?1:-1]

You might ask: How the hell does it work? Well, as the comments explain, it works by using a negative subscript (-1) in an array when the expressions to check for is false, which is of course invalid and will cause a compilation error, otherwise, if the expression is true, a positive subscript is used, which is alright. It does this by declaring (but not creating) an array with the size as the subscript.

Hope someone finds it useful.
john is offline   Reply With Quote
Old 09-09-2004, 08:39 AM   #2
baldurk
DevMaster Staff
 
baldurk's Avatar
 
Join Date: Jan 2003
Location: Mars
Posts: 1,141
Default

what compilers does this work on? I'm guessing it's not standard - does it only work on VC++?
___________________________________________
baldurk
He who knows not and knows that he knows not is ignorant. Teach him.
He who knows not and knows not that he knows not is a fool. Shun him.
baldurk is offline   Reply With Quote
Old 09-09-2004, 11:37 AM   #3
Mihail121
Senior Member
 
Mihail121's Avatar
 
Join Date: Jan 2003
Posts: 868
Default

Agreed with Baldurk here, this trick is more than dangerous and will only work on a compiler or 2. Although the most compilers (Java, etc.) nowdays do it, it's not compiler's responsibility to keep the user safe from himself!
Mihail121 is offline   Reply With Quote
Old 09-09-2004, 12:45 PM   #4
anubis
DevMaster Staff
 
anubis's Avatar
 
Join Date: Apr 2003
Location: Germany
Posts: 2,328
Default

hmm... me thinks that every compiler worth a dime will stop on this...
___________________________________________
If Prolog is the answer, what is the question ?
anubis is offline   Reply With Quote
Old 09-09-2004, 01:54 PM   #5
baldurk
DevMaster Staff
 
baldurk's Avatar
 
Join Date: Jan 2003
Location: Mars
Posts: 1,141
Default

Whoops. Maybe I should look at the code before actually commenting. For some reason I thought it was calling __C_ASSERT__, not declaring an array called __C_ASSERT__.

:X
___________________________________________
baldurk
He who knows not and knows that he knows not is ignorant. Teach him.
He who knows not and knows not that he knows not is a fool. Shun him.
baldurk is offline   Reply With Quote
Old 09-09-2004, 08:09 PM   #6
SnprBoB86
Valued Member
 
Join Date: Aug 2004
Posts: 120
Default

http://www.boost.org/libs/static_ass...tic_assert.htm
___________________________________________
Brandon Bloom
http://brandonbloom.name
SnprBoB86 is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Forum Jump


All times are GMT -7. The time now is 04:28 AM.


Powered by vBulletin
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.