ASSERTS : define « Development « C++ Tutorial






#define DEBUG
 #include <iostream>
 
 #ifndef DEBUG
     #define ASSERT(x)
 #else
     #define ASSERT(x) \
         if (! (x)) \
         { \
             std::cout << "ERROR!! Assert " << #x << " failed\n"; \
             std::cout << " on line " << __LINE__  << "\n"; \
             std::cout << " in file " << __FILE__ << "\n";  \
         }
 #endif
 
 int main()
 {
     int x = 5;
     std::cout << "First assert: \n";
     ASSERT(x==5);
     std::cout << "\nSecond assert: \n";
     ASSERT(x != 5);
     std::cout << "\nDone.\n";
     return 0;
 }
First assert:

Second assert:
ERROR!! Assert x != 5 failed
 on line 24
 in file ASSERTS.cpp

Done.








5.16.define
5.16.1.ASSERTS
5.16.2.function macro of get the max value number
5.16.3.Using parentheses in macros.
5.16.4.Using inline rather than a macro.
5.16.5.A simple assert() macro.
5.16.6.Printing values in DEBUG mode