BOOST_ASSERT_MSG - chunhualiao/public-docs GitHub Wiki
The output is
- test_insertStatement: ../../../../../src/frontend/SageIII/sageInterface/sageInterface.C:14422: void SageInterface::insertStatement(SgStatement*, SgStatement*, bool, bool): Assertion `(false)&&(err_msg.c_str())' failed.
- the err_msg does not show up at all! Useless!!
The build error you encountered is caused by a misuse of the BOOST_ASSERT_MSG
macro in this line:
BOOST_ASSERT_MSG(false, err_msg);
🔍 Explanation of the Problem
According to the Boost 1.69 documentation and the error trace, BOOST_ASSERT_MSG(expr, msg)
expands to:
assert((expr) && (msg));
Here, expr
must be a bool
, and msg
must also be a bool
because of the &&
operator inside assert
.
However, your msg
is a std::string
:
string err_msg = "targetStmt's parent cannot be a " + className + "...";
That leads to this error:
error: no match for ‘operator&&’ (operand types are ‘bool’ and ‘std::string’)
✅ How to Fix It
Use BOOST_FAIL
instead, which accepts a std::string
or const char*
message:
✅ Recommended Fix:
#include <boost/test/floating_point_comparison.hpp> // Or any header that defines BOOST_FAIL
...
BOOST_FAIL(err_msg);
Alternatively, if you're not using Boost's unit testing framework, and BOOST_FAIL
is not available in your setup, fall back to:
✅ Alternative Fix (Standard C++):
std::cerr << err_msg << std::endl;
assert(false);
Or better for debugging:
ROSE_ASSERT_MSG(false, err_msg.c_str());
(If ROSE_ASSERT_MSG(bool, const char*)
is defined similarly to BOOST_ASSERT_MSG
but accepts C-strings.)
💡 Additional Note
It’s good practice to make error messages compile-time safe. Mixing std::string
in macros like assert
or BOOST_ASSERT_MSG
can be risky, since assert()
expects an expression evaluable at runtime as a bool
.