
please wait
|
|
|
|
static_assert
applies only at compile time, therefore, the first argument must be a constant known at compile time. If size and capacity are member variables, then the compiler does not know their values are at compile time.assert
takes only one argument (an int). It does not accept a message argument. The message output when the assertion is raised is usually a token paste of the argument. static_assert const (test::size > test::capacity, "Failed!");
|
|
I want to know how can I print a "Failed!" message if assert condition does not work ? |
|
|
assert( condition && "my cutom message" ) ;
|
|
Assert liberally to document internal assumptions and invariants Summary Be assertive! Use assert or an equivalent liberally to document assumptions internal to a module (i.e., where the caller and callee are maintained by the same person or team) that must always be true and otherwise represent programming errors (e.g.,violations of a function's post-conditions detected by the caller of the function). Ensure that assertions don't perform side effects. Discussion ... It is hard to overestimate the power of assertions. The assert macro and alternatives such as compile-time (and, less preferably, run-time) assertion templates are invaluable tools for detecting and debugging programming errors during a project's development. Of all such tools, they arguably have the best complexity/effectiveness ratio. The success of a project can be conditioned at least in part by the effectiveness with which developers use assertions in their code. ... It is not recommended to throw an exception instead of asserting, even though the standard std::logic_error exception class was originally designed for this purpose. The primary disadvantage of using an exception to report a programming error is that you don't really want stack unwinding to occur — you want the debugger to launch on the exact line where the violation was detected, with the line's state intact. In sum: There are errors that you know might happen. For everything else that shouldn't, and it's the programmer's fault if it does, there is assert. Alexandresu and Sutter in 'C++ Coding Standards - 101 Rules, Guidelines, and Best Practices' |