What does assert do?

Hey,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
#include <assert.h>
#include<list>

using namespace std;

void main(){
list<int> L;
L.push_back(3);
L.push_back(1);
L.push_back(7);
L.push_back(6);

list<int>::iterator result = find(L.begin(), L.end(), 7);
	assert(result == L.end() || *result == 7);/// can you tell me happens here at this line?

}
assert is mostly used in testing programs. Sometimes when you test your code, you want to make sure that at each stage of execution, the program is holding the right values in it's various containers or variables. That is where assert comes in handy. When you "assert" a condition in code, the program will throw an exception when the condition you are testing for is not met. This may cause the program to halt execution or if the exception is handled correctly, the program will simply print a message and continue.

So in the code above, (this doesn't make much sense to me) it is saying, make sure that the find function either did not find a 7 (result == L.end()) or it found a 7 (*result == 7)...so basically, no matter what value the find function returns, the assert will not throw an exception. Which is good...
@ Edward01: I don't want to be a jerk, but what's your excuse for not searching the Reference?

http://www.cplusplus.com/reference/cassert/assert/
Topic archived. No new replies allowed.