C++ boost::assert::BOOST_VERIFY/BOOST_VERIFY_MSG














































C++ boost::assert::BOOST_VERIFY/BOOST_VERIFY_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_VERIFY--

 

The macro BOOST_VERIFY has the same behavior as BOOST_ASSERT, except that the expression that is passed to BOOST_VERIFY is always evaluated. This is useful when the asserted expression has desirable side effects; it can also help suppress warnings about unused variables when the only use of the variable is inside an assertion.


BOOST_ASSERT_MSG can be referred from this link https://cppsecrets.com/users/15001041011099711011610711710997114495051564864103109971051084699111109/C00-boostassertBOOSTASSERTBOOSTASSERTMSG.php


BOOST_DISABLE_ASSERTS/NDEBUG: BOOST_DISABLE_ASSERTS and NDEBUG can generate the same result as  BOOST_VERIFY(expr) expands to ((void)(expr)) on the other hand BOOST_ASSERT(expr) expands to ((void)0)


#define NDEBUG #include <boost/assert.hpp> #include <iostream> using namespace std; int main() { int b=1,a=1; BOOST_VERIFY((a == 2) ? (b = 10) : (b = 20)); cout << "After Verify b=" << b << endl; BOOST_ASSERT((a == 2) ? (b = 30) : (b = 40)); cout << "After Assert b="<<b; }

Output: After Verify b=20

After Assert b=20


#define BOOST_DISABLE_ASSERTS #include <boost/assert.hpp> #include <iostream> using namespace std; int main() { int b=1,a=1; BOOST_VERIFY((a == 2) ? (b = 10) : (b = 20)); cout << "After Verify b=" << b << endl; BOOST_ASSERT((a == 2) ? (b = 30) : (b = 40)); cout << "After Assert b="<<b; }

Output: After Verify b=20

After Assert b=20


BOOST_ENABLE_ASSERT_HANDLER : If the macro BOOST_ENABLE_ASSERT_HANDLER is defined when <boost/assert.hpp> is included, BOOST_VERIFY(expr) expands to BOOST_ASSERT(expr) and will generate same result.

 

#define BOOST_ENABLE_ASSERT_HANDLER #include <boost/assert.hpp> #include <iostream> 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 b=1,a=1; BOOST_VERIFY((a == 2) ? (b=10) : (b = 20)); cout << "After Verify b=" << b<<endl; BOOST_ASSERT((a == 2) ? (b = 30) : (b = 40)); cout << "After Assert b="<<b; }

Output: After Verify b=20

After Assert b=40



--BOOST_VERIFY_MSG--


The macro BOOST_VERIFY_MSG is similar to BOOST_VERIFY, with an additional parameter, an error message.

#include <boost/assert.hpp> #include <iostream> using namespace std; int main() { int a = 1; BOOST_VERIFY_MSG((a == 2), "Error in value a"); }

Output: Assertion failed: ((a == 2))&&("Error in value a"), file C:\Users\pendu\source\repos\boost_assert\boost_assert.cpp, line 7


BOOST_DISABLE_ASSERTS/NDEBUG: If the macro BOOST_DISABLE_ASSERTS is defined when <boost/assert.hpp> is included, BOOST_VERIFY_MSG(expr,msg) expands to ((void)(expr)).

BOOST_VERIFY_MSG(expr,msg) expands to ((void)(expr)) when NDEBUG is defined, to BOOST_ASSERT_MSG(expr,msg) when it%u2019s not.


#define BOOST_DISABLE_ASSERTS #include <boost/assert.hpp> #include <iostream> using namespace std; int main() { int b=1,a=1; BOOST_VERIFY_MSG(((a == 2) ? (b = 10) : (b = 20)),"verify"); cout << "After Verify b="<<b<<endl; BOOST_ASSERT_MSG(((a == 2) ? (b = 30) : (b = 40)),"assert"); cout << "After Assert b="<<b; }

Output: After Verify b=20

After Assert b=20


#define NDEBUG #include <boost/assert.hpp> #include <iostream> using namespace std; int main() { int b=1,a=1; BOOST_VERIFY_MSG(((a == 2) ? (b = 10) : (b = 20)),"verify"); cout << "After Verify b="<<b<<endl; BOOST_ASSERT_MSG(((a == 2) ? (b = 30) : (b = 40)),"assert"); cout << "After Assert b="<<b; }

Output: After Verify b=20

After Assert b=20


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

BOOST_ASSERT_MSG can be referred from this link https://cppsecrets.com/users/15001041011099711011610711710997114495051564864103109971051084699111109/C00-boostassertBOOSTASSERTBOOSTASSERTMSG.php


Comments