Custom Assertion Handler#
Note
The availability of the extension can be checked with the TBB_EXT_CUSTOM_ASSERTION_HANDLER
macro defined in
oneapi/tbb/global_control.h
and oneapi/tbb/version.h
headers.
Description#
oneTBB implements assertion checking that detects errors in header files and library code. While most assertions are
active in debug builds (controlled by TBB_USE_ASSERT
), some assertions remain present in release builds. By
default, failed assertions display an error message and terminate the application. The custom assertion handler
mechanism extends this by allowing developers to implement their own assertion handling functions. The API is
semantically similar to the standard std::set_terminate
and std::get_terminate
functions.
API#
Header#
#include <oneapi/tbb/global_control.h>
Synopsis#
#define TBB_EXT_CUSTOM_ASSERTION_HANDLER 202510
namespace oneapi {
namespace tbb {
namespace ext {
using assertion_handler_type = void(*)(const char* location, int line,
const char* expression, const char* comment);
assertion_handler_type set_assertion_handler(assertion_handler_type new_handler) noexcept;
assertion_handler_type get_assertion_handler() noexcept;
}}}
Types#
-
type assertion_handler_type#
Type alias for the pointer to an assertion handler function.
Functions#
-
assertion_handler_type set_assertion_handler(assertion_handler_type new_handler) noexcept#
Sets the provided assertion handler and returns the previous handler. If new_handler
is nullptr
, resets to the
default handler.
Note
new_handler
must not return. If it does, the behavior is undefined.
-
assertion_handler_type get_assertion_handler() noexcept#
Returns the current assertion handler.
Example#
#include <oneapi/tbb/global_control.h>
#include <fstream>
#ifdef TBB_EXT_CUSTOM_ASSERTION_HANDLER
auto default_handler = tbb::ext::get_assertion_handler();
void wrapper_assertion_handler(const char* location, int line,
const char* expression, const char* comment) {
// Execute a custom step before the default handler: log the assertion
std::ofstream log{"example.log", std::ios::app};
log << "Function: " << location << ", line: " << line << ", assertion "
<< expression << " failed: " << comment << "\n";
log.close();
default_handler(location, line, expression, comment);
}
#endif // TBB_EXT_CUSTOM_ASSERTION_HANDLER
int main() {
#ifdef TBB_EXT_CUSTOM_ASSERTION_HANDLER
// Set custom handler
tbb::ext::set_assertion_handler(wrapper_assertion_handler);
#endif // TBB_EXT_CUSTOM_ASSERTION_HANDLER
// Use oneTBB normally - any assertion failures will use custom handler
// ...
#ifdef TBB_EXT_CUSTOM_ASSERTION_HANDLER
// Restore the default handler
tbb::ext::set_assertion_handler(nullptr);
#endif // TBB_EXT_CUSTOM_ASSERTION_HANDLER
}
See also