![]() |
| [[ Home | Forums | 3D Engines Database | Wiki | Articles/Tutorials | Game Dev Jobs | IRC Chat Network | Contact Us ]] |
|
|
#1 |
|
Member
Join Date: Jan 2003
Posts: 85
|
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:
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. |
|
|
|
|
|
#2 |
|
DevMaster Staff
Join Date: Jan 2003
Location: Mars
Posts: 1,141
|
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. |
|
|
|
|
|
#3 |
|
Senior Member
Join Date: Jan 2003
Posts: 868
|
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!
|
|
|
|
|
|
#4 |
|
DevMaster Staff
Join Date: Apr 2003
Location: Germany
Posts: 2,328
|
hmm... me thinks that every compiler worth a dime will stop on this...
___________________________________________
If Prolog is the answer, what is the question ? |
|
|
|
|
|
#5 |
|
DevMaster Staff
Join Date: Jan 2003
Location: Mars
Posts: 1,141
|
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. |
|
|
|
|
|
#6 |
|
Valued Member
Join Date: Aug 2004
Posts: 120
|
|
|
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|