file
Assert.hMacro CORRADE_
Namespaces
- namespace Corrade
- Root namespace.
- namespace Corrade::Utility
- Utilities.
Defines
- #define CORRADE_NO_ASSERT
- Disable all assertions.
- #define CORRADE_GRACEFUL_ASSERT
- Gracefully print assertions.
- #define CORRADE_STANDARD_ASSERT
- Use standard assert.
- #define CORRADE_ASSERT(condition, message, returnValue)
- Assertion macro.
- #define CORRADE_CONSTEXPR_ASSERT(condition, message)
- Constexpr assertion macro.
- #define CORRADE_ASSERT_OUTPUT(call, message, returnValue)
- Call output assertion macro.
- #define CORRADE_ASSERT_UNREACHABLE(message, returnValue)
- Assert that the code is unreachable.
- #define CORRADE_INTERNAL_ASSERT(condition)
- Internal assertion macro.
- #define CORRADE_INTERNAL_CONSTEXPR_ASSERT(condition)
- Internal constexpr assertion macro.
- #define CORRADE_INTERNAL_ASSERT_OUTPUT(call)
- Internal call output assertion macro.
- #define CORRADE_INTERNAL_ASSERT_EXPRESSION(...) new in Git master
- Internal expression assertion macro.
- #define CORRADE_INTERNAL_ASSERT_UNREACHABLE() new in 2020.06
- Internal assert that the code is unreachable.
Define documentation
#define CORRADE_NO_ASSERT
Disable all assertions.
This macro is not defined by Corrade, but rather meant to be defined by the user. When defined, assertions are not checked at all. See documentation of CORRADE_
#define CORRADE_GRACEFUL_ASSERT
Gracefully print assertions.
This macro is not defined by Corrade, but rather meant to be defined by the user. Unlike CORRADE_
When both CORRADE_
#define CORRADE_STANDARD_ASSERT
Use standard assert.
This macro is not defined by Corrade, but rather meant to be defined by the user. This macro causes all CORRADE_assert()
, using just the expression and discarding the message, if any. This makes them more lightweight, since Corrade::
When this macro is defined, CORRADE_NDEBUG
macro have the same effect.
#define CORRADE_ASSERT(condition, message, returnValue)
Assertion macro.
Parameters | |
---|---|
condition | Assert condition |
message | Message on assertion fail |
returnValue | Return value on assertion fail |
Usable for sanity checks on user input, as it prints explanational message on error.
By default, if assertion fails, message
is printed to error output and the application aborts. If CORRADE_returnValue
instead of aborting. If CORRADE_assert(condition)
, ignoring message
. If CORRADE_NDEBUG
are defined), this macro expands to do {} while(false)
. Example usage:
T operator[](std::size_t pos) const { CORRADE_ASSERT(pos < size(), "Array::operator[](): index out of range", {}); return data[pos]; }
If the function has return type void
, just use an empty parameter (allowed in C++11):
void compile() { CORRADE_ASSERT(!sources.isEmpty(), "Shader::compile(): no sources added", ); // ... }
You can use stream output operators for formatting just like when printing to Corrade::
CORRADE_ASSERT(pos < size(), "Array::operator[](): accessing element" << pos << "in an array of size" << size(), {});
#define CORRADE_CONSTEXPR_ASSERT(condition, message)
Constexpr assertion macro.
Parameters | |
---|---|
condition | Assert condition |
message | Message on assertion fail |
Unlike CORRADE_constexpr
functions like this:
constexpr int divide(int a, int b) { return CORRADE_CONSTEXPR_ASSERT(b, "divide(): can't divide by zero"), a/b; }
In a constexpr
context, if assertion fails, the code fails to compile. In a non- constexpr
context, if assertion fails, message
is printed to error output and the application aborts. If CORRADE_message
is ignored and the standard assert()
is called if condition
fails. If CORRADE_NDEBUG
are defined), this macro expands to static_cast<void>(0)
.
As with CORRADE_
The implementation is based on the Asserts in constexpr functions article by Andrzej Krzemieński and the followup discussion.
You can override this implementation by placing your own #define CORRADE_CONSTEXPR_ASSERT
before including the Corrade/
#define CORRADE_ASSERT_OUTPUT(call, message, returnValue)
Call output assertion macro.
Parameters | |
---|---|
call | Assert call |
message | Message on assertion fail |
returnValue | Return value on assertion fail |
Unlike CORRADE_NDEBUG
are defined), making it usable for checking function output. Otherwise the behavior is the same as with CORRADE_
CORRADE_ASSERT_OUTPUT(initialize(userParam), "Initialization failed: wrong parameter" << userParam, );
You can override this implementation by placing your own #define CORRADE_ASSERT_OUTPUT
before including the Corrade/
#define CORRADE_ASSERT_UNREACHABLE(message, returnValue)
Assert that the code is unreachable.
Parameters | |
---|---|
message | Message on assertion fail |
returnValue | Return value on assertion fail |
By default, if code marked with this macro is reached, message
is printed to error output and the application aborts. If CORRADE_returnValue
instead of aborting. If CORRADE_assert(!"unreachable code")
. If CORRADE_NDEBUG
are defined), this macro hints to the compiler that given code is not reachable, possibly helping the optimizer (using a compiler builtin on GCC, Clang and MSVC; calling std::return
statement can thus be safely omitted in a code path following this macro without causing any compiler warnings or errors. Example usage:
std::string statusString(Status status) { switch(status) { case Status::Great: return "great"; case Status::NotGreat: return "not great"; } CORRADE_ASSERT_UNREACHABLE("status is neither great nor non-great", {}); }
You can override this implementation by placing your own #define CORRADE_ASSERT_UNREACHABLE
before including the Corrade/
#define CORRADE_INTERNAL_ASSERT(condition)
Internal assertion macro.
Parameters | |
---|---|
condition | Assert condition |
Unlike CORRADE_
By default, if assertion fails, failed condition, file and line is printed to error output and the application aborts. If CORRADE_assert(condition)
. If CORRADE_NDEBUG
are defined), this macro expands to do {} while(false)
. Example usage:
CORRADE_INTERNAL_ASSERT(pos < size());
#define CORRADE_INTERNAL_CONSTEXPR_ASSERT(condition)
Internal constexpr assertion macro.
Parameters | |
---|---|
condition | Assert condition |
Unlike CORRADE_constexpr
functions like this:
constexpr int divide(int a, int b) { return CORRADE_INTERNAL_CONSTEXPR_ASSERT(b), a/b; }
In a constexpr
context, if assertion fails, the code fails to compile. In a non- constexpr
context, if assertion fails, failed condition, file and line is printed to error output and the application aborts. If CORRADE_assert()
is called if condition
fails. If CORRADE_NDEBUG
are defined), this macro expands to static_cast<void>(0)
.
You can override this implementation by placing your own #define CORRADE_INTERNAL_CONSTEXPR_ASSERT
before including the Corrade/
#define CORRADE_INTERNAL_ASSERT_OUTPUT(call)
Internal call output assertion macro.
Parameters | |
---|---|
call | Assert call |
Unlike CORRADE_NDEBUG
are defined), making it usable for checking function output. Otherwise the behavior is the same as with CORRADE_
CORRADE_INTERNAL_ASSERT_OUTPUT(initialize());
You can override this implementation by placing your own #define CORRADE_INTERNAL_ASSERT_OUTPUT
before including the Corrade/
#define CORRADE_INTERNAL_ASSERT_EXPRESSION(...) new in Git master
Internal expression assertion macro.
A variant of CORRADE_
Containers::Optional<Containers::Array<char>> data; CORRADE_INTERNAL_ASSERT_OUTPUT(data = Utility::Path::read("file.dat")); consume(*std::move(data));
Could be rewritten in a shorter way and without having to use std::
consume(*CORRADE_INTERNAL_ASSERT_EXPRESSION(Utility::Path::read("file.dat")));
The macro passes the expression to a function which asserts it evaluates to true
and then returns the value forwarded. That implies the expression result type has to be at least movable. If CORRADE_assert(value)
inside, unfortunately it's not possible for the standard assert macro to show the expression. If CORRADE_NDEBUG
are defined), this macro expands to nothing, leaving just the parenthesized expression out of it.
You can override this implementation by placing your own #define CORRADE_INTERNAL_ASSERT_EXPRESSION
before including the Corrade/
#define CORRADE_INTERNAL_ASSERT_UNREACHABLE() new in 2020.06
Internal assert that the code is unreachable.
Compared to CORRADE_
By default, if code marked with this macro is reached, message with file and line is printed to error output and the application aborts. If CORRADE_assert(!"unreachable code")
. If CORRADE_NDEBUG
are defined), this macro hints to the compiler that given code is not reachable, possibly helping the optimizer (using a compiler builtin on GCC, Clang and MSVC; calling std::return
statement can thus be safely omitted in a code path following this macro without causing any compiler warnings or errors. Example usage:
std::size_t elementCount(std::size_t size, Type type) { switch(type) { case Type::UnsignedInt: return size/4; case Type::UnsignedShort: return size/2; case Type::UnsignedByte: return size/1; } CORRADE_INTERNAL_ASSERT_UNREACHABLE(); }
You can override this implementation by placing your own #define CORRADE_INTERNAL_ASSERT_UNREACHABLE
before including the Corrade/