How are assertions done in c++? Example code is appreciated.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Asserts are a way of explicitly checking the assumptions that your code makes, which helps you track down lots of bugs by narrowing down what the possible problems could be. They are typically only evaluated in a special ‘debug’ build of your application, so they won’t slow down the final release version.
Let’s say you wrote a function that took a pointer as an argument. There’s a good chance that your code will assume that the pointer is non-NULL, so why not explicitly check that with an assertion? Here’s how:
An important thing to note is that the expressions you assert must never have side effects, since they won’t be present in the release build. So never do something like this:
Some people also like to add little messages into their assertions to help give them meaning. Since a string always evaulates to true, you could write this: