C++ boost::assert::BOOST_ASSERT/BOOST_ASSERT_MSG














































C++ boost::assert::BOOST_ASSERT/BOOST_ASSERT_MSG



Description: 
The header <boost/assert.hpp> defines the macro BOOST_ASSERT, which is similar to the standard assert macro defined in <cassert>. The
macro is intended to be used in both Boost libraries and user code.

--BOOST_ASSERT--


By default, BOOST_ASSERT(expr) expands to assert(expr).

#include <boost/assert.hpp> #include <iostream> using namespace std; int main() { int values[10] = { 1,2,3,4,5,6,7,8,9,10 }; BOOST_ASSERT(values[0] == 1); BOOST_ASSERT(values[9] == 14); cout << "running"; }

Output: Assertion failed: values[9] == 14, file C:\Users\pendu\source\repos\boost_assert\boost_assert.cpp, line 8


NDEBUG: Assertion macro is disabled if, at the moment of including <assert.h>, a macro with the name NDEBUG has already been defined. This allows for a coder to include as many assert calls as needed in source code while debugging the program and then disable all of them for the production version by simply including a line like #define NDEBUG at the beginning of the code, before the inclusion of <assert.h>.

#define NDEBUG #include <boost/assert.hpp> #include <iostream> using namespace std; int main() { int values[10] = { 1,2,3,4,5,6,7,8,9,10 }; BOOST_ASSERT(values[0] == 1); BOOST_ASSERT(values[9] == 14); cout << "running"; }

Output: running


BOOST_DISABLE_ASSERTS: If the macro BOOST_DISABLE_ASSERTS is defined when <boost/assert.hpp> is included, BOOST_ASSERT(expr) expands to ((void)0), regardless of whether the macro NDEBUG is defined. This allows users to selectively disable BOOST_ASSERT without affecting the definition of the standard assert.

#define BOOST_DISABLE_ASSERTS #include <boost/assert.hpp> #include <iostream> #include <cassert> using namespace std; int main() { int values[10] = { 1,2,3,4,5,6,7,8,9,10 }; BOOST_ASSERT(values[0] == 1); BOOST_ASSERT(values[9] == 14); cout << "running"<<endl; assert(values[0] == 2); }

Output: running Assertion failed: values[0] == 2, file C:\Users\pendu\source\repos\boost_assert\boost_assert.cpp, line 12


BOOST_ENABLE_ASSERT_HANDLER: If the macro BOOST_ENABLE_ASSERT_HANDLER is defined when <boost/assert.hpp> is included, BOOST_ASSERT(expr) expands to

(BOOST_LIKELY(!!(expr))? ((void)0): ::boost::assertion_failed(#expr, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__))

User has to define assertion_failed() with proper signature.

#define BOOST_ENABLE_ASSERT_HANDLER #include <boost/assert.hpp> #include <iostream> #include <cassert> using namespace std; namespace boost { void assertion_failed(char const* expr, char const* function, char const* file, long line) { cout << "User defined handler, error in line "<<line; } } int main() { int values[10] = { 1,2,3,4,5,6,7,8,9,10 }; BOOST_ASSERT(values[0] == 1); BOOST_ASSERT(values[9] == 14); }

Output: User defined handler, error in line 17


BOOST_ENABLE_ASSERT_DEBUG_HANDLER: If the macro BOOST_ENABLE_ASSERT_DEBUG_HANDLER is defined when <boost/assert.hpp> is included, BOOST_ASSERT(expr) expands to ((void)0) when NDEBUG is defined. Otherwise the behavior is as if BOOST_ENABLE_ASSERT_HANDLER has been defined.


--BOOST_ASSERT_MSG--


By default, BOOST_ASSERT_MSG(expr,msg) expands to assert((expr)&&(msg)).


#include <boost/assert.hpp> #include <iostream> using namespace std; int main() { int values[10] = { 1,2,3,4,5,6,7,8,9,10 }; BOOST_ASSERT_MSG((values[9] == 1), "Error in values[0]"); BOOST_ASSERT_MSG((values[9] == 12), "Error in value[9]"); }

Output: Assertion failed: ((values[9] == 1))&&("Error in values[0]"), file C:\Users\pendu\source\repos\boost_assert\boost_assert.cpp, line 7


BOOST_DISABLE_ASSERTSIf the macro BOOST_DISABLE_ASSERTS is defined when <boost/assert.hpp> is included, BOOST_ASSERT_MSG(expr,msg) expands to ((void)0), regardless of whether the macro NDEBUG is defined.

BOOST_ENABLE_ASSERT_HANDLER: If the macro BOOST_ENABLE_ASSERT_HANDLER is defined when <boost/assert.hpp> is included, BOOST_ASSERT_MSG(expr,msg) expands to


(BOOST_LIKELY(!!(expr))? ((void)0): ::boost::assertion_failed_msg(#expr, msg, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__))


This is true regardless of whether NDEBUG is defined. 

boost::assertion_failed_msg is declared in <boost/assert.hpp> as

namespace boost { void assertion_failed_msg(char const * expr, char const * msg, char const * function, char const * file, long line); }

but it is never defined. The user is expected to supply an appropriate definition.


BOOST_ENABLE_ASSERT_DEBUG_HANDLER:If the macro BOOST_ENABLE_ASSERT_DEBUG_HANDLER is defined when <boost/assert.hpp> is included, BOOST_ASSERT_MSG(expr) expands to ((void)0) when NDEBUG is defined. Otherwise the behavior is as if BOOST_ENABLE_ASSERT_HANDLER has been defined.

Note: The macro BOOST_ASSERT_MSG is similar to BOOST_ASSERT, but it takes an additional argument, a character literal, supplying an error message. Refer above for examples.


Comments