Write secure code with assertions (assert and static_assert) | Modern Cpp Series Ep. 87

preview_player
Показать описание

►Lesson Description: In this lesson I will show you one way to catch bugs at run-time, and one way to catch bugs at compile-time. The assert statement (from C) is available to us in C++. Assertions serve as contracts of thing that must absolutely be true, and if they're not, the safest thing to do is for our program to terminate. In modern C++ we have available to use static_assert which can check boolean-constexpr expressions to see if they pass the test, and catching bugs at compile-time is often much easier on us in the long run!

►Please like and subscribe to help the channel!
Рекомендации по теме
Комментарии
Автор

Thanks; Very Kind small examples “ the big things came out of tiny things”. Thanks again for the methodology of how to understand and learn something.

__hannibaalbarca__
Автор

It's just a matter of time before someone comes up with a programming language called "42" 😂 Great explanation Mike!

GaryChike
Автор

Thank you very much. Sorry if stupid question, how do these asserts relate to the ones in Google's test framework asserts... If any?

VoidloniXaarii
Автор

How do you setup your C++ code such that it can be tested on an ad hoc basis? E.g. Java has maven with test profiles; I struggle a bit doing something similar with C++...I guess one has to resort to CMake (or similar build environments) to invoke test suits (?) And D has compiler switches which invoke unit tests.

bsdooby
Автор

Great video! But)
From my experience in the field, c assert() is a bad thing for the project. In Release variant asserts are compiled out, so you miss the checks. The checks were performed only in Debug variant. So you have different code between Debug and Release variants.

C asserts can work good only in a big company, where they do extensive testing for debug AND release variants.

My best practice: just do not use c asserts. If there is a suspect for an existence of an error, perform a mature param check with if (...) {print log, handle error / throw exception} and just avoid using c asserts...

But static_assert is a great stuff)

fatty-it-man