A simple assert() macro. : define « Development « C++ Tutorial






#define DEBUG
#include <iostream>
using namespace std;
#ifndef DEBUG
   #define ASSERT(x)
#else
   #define ASSERT(x) \
            if (! (x)) \
            { \
               cout << "ERROR!! Assert " << #x << " failed\n"; \
               cout << " on line " << __LINE__  << "\n"; \
               cout << " in file " << __FILE__ << "\n";  \
            }
#endif


int main()
{
   int x = 5;
   cout << "First assert: \n";
   ASSERT(x==5);
   cout << "\nSecond assert: \n";
   ASSERT(x != 5);
   cout << "\nDone.\n";
 return 0;
}








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