PrevUpHomeNext

utx::rt_assert


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

Class

class rt_assert;

Constructor

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

Removed constructors and methods

rt_assert() = delete;
rt_assert(const utx::rt_assert &) = delete;
rt_assert(const utx::rt_assert &&) = delete;
utx::rt_assert & operator=(const utx::rt_assert &) = delete;
utx::rt_assert & operator=(const utx::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 utx::rt_assert is used inside a try block, the catch block will catch the exception thrown by utx::rt_assert
  • If utx::rt_assert is not used inside a try block, the program will cause a core dump.

Example

#include <utxcpp/core.hpp>

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

Result:

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

PrevUpHomeNext