PrevUpHomeNext

esv::rt_assert


Children's playmates.

esv::rt_assert is a c++ alternative of assert, with c++ exception support.

Class

class rt_assert;

Constructor

esv::rt_assert::rt_assert(bool condition, const std::string_view msg = "");

Removed constructors and methods

rt_assert() = delete;
rt_assert(const esv::rt_assert &) = delete;
rt_assert(const esv::rt_assert &&) = delete;
esv::rt_assert & operator=(const esv::rt_assert &) = delete;
esv::rt_assert & operator=(const esv::rt_assert &&) = delete;

Notice

[Note] Note
  • This class has no member variables.
  • If you inherit this class, it's not recommended to add member variables.
  • If esv::rt_assert is used inside a try block, the catch block will catch the exception thrown by esv::rt_assert
  • If esv::rt_assert is not used inside a try block, the program will cause a core dump.

Example

#include <esvcpp/core.hpp>

int main() {
	try {
		esv::rt_assert{true}; // pass
		esv::rt_assert(false); // print a short msg to std::cerr, and throw an exception
	} catch (std::exception & exc) {
		esv::printe("[esv::rt_assert]", exc.what());
	}
	try {
		esv::rt_assert(2+3==7, "Does 2+3==7?"); // print a short msg to std::cerr, and throw an exception
	} catch (std::exception & exc) {
		esv::printe("[esv::rt_assert]", exc.what());
	}
	{
		int x = 3;
		int y = x*4;
		esv::rt_assert(y==7, "Does y==7?"); // print a short msg to std::cerr, throw an exception, and cause a core dump.
	}
}

Result:

esv::rt_assert(...) failed!
[esv::rt_assert] esv::rt_assert(...) failed! Your condition [  ] is false!
esv::rt_assert(...) failed!
[esv::rt_assert] esv::rt_assert(...) failed! Your condition [ Does 2+3==7? ] is false!
esv::rt_assert(...) failed!
Abort trap (core dumped)

PrevUpHomeNext

E