Question, About Assert, is it basically an if statement??
if extention == "mp3"
do this blah blah
Assert ( extension == "mp3" )
do this blah blah blah
Assert ( extention == "ms3d" )
do this blahblahbalh
Is that about it?
Question, About Assert, is it basically an if statement??
if extention == "mp3"
do this blah blah
Assert ( extension == "mp3" )
do this blah blah blah
Assert ( extention == "ms3d" )
do this blahblahbalh
Is that about it?
Sometimes I forget what I am doing when I enter a room, actually, quite often.
It's sort of like an if, because if the condition is false, it throws an assert (stops your program, and a window pops up).
It is similar to an if statement, except that is conditionally compiled. That is, an if statement that can magically remove itself from the code.
http://www.embedded.com/story/OEG20010311S0021
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
No, you should not think of it as an 'if'. Think of it as a call to a function taking a bool parameter. (yes I know it's not quite that either)
Only use it to check things that should always be true. The example you give is not what an assert should be used for.
You can't use == like that to compare an array of chars either. You need to use strcmp, or one of the many other flavours of it.
<quibble> If the variable extention is a std::string, you can do that comparison, because the literal string will be converted implicitly</quibble>Originally Posted by iMalc
I'm thinking of using it to check what extension a string name of type Filename gives me.
if filename == "foo.mp3"
std::string extension something syntactically correct
assert (exstension == "mp3)
do stuff
assert (extension == "ms3d")
do stuff..
Sometimes I forget what I am doing when I enter a room, actually, quite often.
> I'm thinking of using it to check what extension a string name of type Filename gives me.
Never use assert() for checking user input.
1. It is compiled out when you do a release build.
2. It kills the program in a debug build.
Neither of these are desirable.
Assert is for checking program logic, for example
Code:int mySqrt ( int x ) { assert( x >= 0 ); /* make sure it's positive! */ }
Ahh, I see..
Well, it isn't user input... I'm working with an internal resource manager. So assert might still be of use in some situation.
Sometimes I forget what I am doing when I enter a room, actually, quite often.
Ideally, the contents of an assert should never be false. So never put something into an assert that you expect to be false. It is only meant for debugging to ensure that the data you expect is there.
As has been said, it is compiled out during a release build, so don't do any important calculations or function calls within the assert. For example, the return value of std::set::insert is a pair that has an iterator and a bool. The bool indicates whether the insert succeeded or failed. One might be tempted to assert(mySet.insert(5).second); to ensure the value is not a duplicate. That would be a bad idea because the entire contents of the assert including the insert call would be removed in the release build and the insert would never happen.
Also, the pseudocode:
assert (exstension == "mp3)
do stuff
assert (extension == "ms3d")
do stuff..
is not correct because in that code no matter what one of the asserts will fire (the extension cannot be both "mp3" and "ms3d"). If you are inside a function that only works with mp3 extensions, and you have other code that is supposed to do the error checking to verify that it is mp3, then maybe you would add an assert(extension == "mp3"); into that function because that code always expects to have an mp3 extension.