Category : assert

Consider the following code example, a simple template class wrapper with basic overloaded arithmetic operators. In this class’s operator/ I’m using the ternary operator to throw an exception if division by 0 is detected otherwise, I’m returning the calcuation. some.h #pragma once #include <cassert> #include <stdexcept> template<typename T> struct Var { T var; // … ..

Read more

If one is using exceptions to replace assertions for debugging/testing purposes. Does it make sense to declare a function noexcept conditionally on the testing flag or this would mess too much with the function interface? #ifdef MYTESTING #define MYASSERT(x) if(not x) throw std::logic_error("error not "#x) #else #define MYASSERT(x) assert(x); #endif double f(double x) #if not ..

Read more

I found myself with this type of code, template<class T> void f(T const& t){ if constexpr(std::is_same<T, double>{}) call_a(t); // call_a only compiles for double else if constexpr(std::is_same<T, float >{}) call_b(t); // call_b only compiles for float else assert(0 && "not implemented"); } What would be an idiomatic way to produce a compiler error instead of ..

Read more

My assert function does not stop the program from continuing on. I do not have NDEBUG defined as I’ve seen from other questions on this matter. Can anyone explain why? #include <iostream> #include <assert.h> int main() { assert(0==1); std::cout << "Execution continues past the first assert" << std::endl; return 0; } Source: Windows Que..

Read more

My assert does not stop the program from continuing on. I do not have NDEBUG defined as I’ve seen from other questions on this matter. Can anyone explain why? #include <iostream> #include <assert.h> int main() { assert(0==1); std::cout << "Execution continues past the first assert" << std::endl; return 0; } Edit: I am using VS2019 ..

Read more