assert()
Macro
<cassert>
for C++.
assert()
macro.
assert()
Macroassert()
macro takes a single expression as an argument.
$ cat t.cc #include <cassert> int main() { const int two = 2; assert(two < 1); } $ g++ -o t t.cc $ ./t Assertion failed: two < 1, file t.cc, line 5 Abort(coredump) $
NDEBUG
.
g++ -DNDEBUG ...
or in your code (before the <cassert>
).
#define NDEBUG #include <cassert>
assert
macros.
Wrong:
// The file must exist, I just created it.assert(ifs.open(fname));
Right:
// The file must exist, I just created it. const bool ok = ifs.open(fname); assert(ok);
std::string inp; std::cin >> inp;if (inp[0] == '#') { ... }
std::string inp; std::cin >> inp; assert(inp.size() > 0); if (inp[0] == '#') { ... }
// Input is never empty. assert(inp.size() > 0);
switch (dir) { case north: // whatever case east: // whatever case south: // whatever case west: // whatever }
assert(!"whatever...");
switch (dir) { case north: // whatever case east: // whatever case south: // whatever case west: // whatever default: assert(!"impossible direction") }
if
and case
statements make understanding tricker.
This page last modified on 17 February 2004.