We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create assertions to test programming, expensive and impossible conditions inside methods.
The text was updated successfully, but these errors were encountered:
Tasks:
default
Sorry, something went wrong.
Examples:
Switch-case:
enum class Season { Spring, Summer, Fall, Winter, }; enum class Weather { Hot, Cold }; Weather SeasonToWeather(Season season) { switch (season) { case Season::Spring: // Fall through case Season::Summer: return Weather::Hot; case Season::Fall: // Fall through case Season::Winter: return Weather::Cold; default: assert("Impossible condition in switch-case"); return {}; } }
Number range:
using Probability = double; inline bool is_probability(Probability prob) { return prob >= 0 && prob <= 1; } Probability drawProbability() { static std::mt19937 rng; static std::uniform_real_distribution<Probability> distribution(0.0, 1.0); auto prob = distribution(rng); assert(is_probability(prob)); return prob; }
Nullity:
template<typename T> inline bool not_null(const std::unique_ptr<T> &ptr) { return ptr != nullptr; } template<typename T> inline bool not_null(const std::shared_ptr<T> &ptr) { return ptr != nullptr; } template<typename Ptr> inline bool not_null(const Ptr &ptr) { static_assert(std::is_pointer<Ptr>::value, "Should be a pointer type"); return ptr != nullptr; } class Object {}; std::shared_ptr<Object> createObject() { auto created = std::make_shared<Object>(); assert(not_null(created)); return created; }
tenoriobruno
No branches or pull requests
Create assertions to test programming, expensive and impossible conditions inside methods.
The text was updated successfully, but these errors were encountered: