A programmer should often use assertions, so he is able to detect errors as soon as they occur.

In C++ the standard assert-macro doesn’t allow for an additional message. But with a simple workaround, you can print out additional information when the assertion occurs:

#include <iostream>
#include <cassert>

using namespace std;

int main (int argc, char * const argv[]) {
    int x = 30;
    int y = 40;

    // the following line is similar to: assert(x > y && true);
    assert(x > y && "x is not bigger than y");

    return 0;
}

Another option is to write an own macro, that allows a second message parameter. The macro can look like that:

#define myAssert(cond, msg) 
    if ((cond) == false) { 
        std::cerr << "ASSERT: " << #cond << " ### " << msg 
            << " on line " << __LINE__ << " in file: " 
            << __FILE__ << std::endl; 
    }

Add your comment now