Quote:Original post by The C modest god
Quote:Original post by The C modest god
What is an assertion?
Ok, I have read about assert in bjarne's book.
First, it is some sort of C backward compatability thing?
It came from C, but it's still a valid debugging tool in C++ programs. Basically, if you assert( true );, you're saying: "I expect this to be true. If it's not a serious bug has occured, please tell me so I can track it down better."
Quote:It doesn't work well in templates.
Bullshit, I use them in templates just as easily as I do in other functions.
Quote:It gives the line in which it was called, but that is useless if you encapsulate it, which you propabbly would.
Huh? Elaborate what you're talking about?
Quote:It just calls a function? it would not call all the destructors?
In release mode, it does
nothing. In debug mode, it halts the execution of the program and tells you where it's happened.
Quote:So it is not suitable to deal a bug which happened in the end user's computer,
Correct to some degree - end users should be getting things compiled in release mode - which means none of the asserts will trigger. It dosn't clean up very nicely, but it does find problems in your code very nicely, which is it's entire point.
Quote:So I don't see why not to use exceptions to catch logical bugs (which usually develope later to a much horrific bugs) on the end user's computer.
Exceptions are for error occurances, not necessairly bugs. For example, not being able to load a file because it does not exist is not a bug - but it may be an exception, especially if the program can't continue without it. Exceptions are for errors that need to get "handled".
An example of a bug would be a pointer to the screen being NULL when it shouldn't. Then you would likely use an assert, rather than trying to display an error message on the screen... when you don't know where the screen is. Asserts are for errors that need to be pointed out, and can't/shouldn't be handled.