Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert with dynamic message?

Tags:

c++

assert

boost

In my program I want to use asserts that show an error message. Apart from the well known workarounds for C and C++ there's the "real" solution as BOOST offers BOOST_ASSERT_MSG( expr, msg ) (see also assert() with message)

But a static message isn't enough for me, I also want to show sometimes the failed variables, e.g. in a case like

BOOST_ASSERT_MSG( length >= 0, "No positive length found! It is " << length )

As you can see I'd like to format the message "string" as an stringstream or ostream as that'd allow me to easily show custom types (assuming I've defined the relevant formating function).

The problem here is that BOOST_ASSERT_MSG is by default requiring a char const * so that's not compatible.

Is there a way to redefine / overload assertion_failed_msg() in such a way that using a stream as message will work? How?
(My naive approach failed as the compiler first wanted to do an operator<<("foo",bar) on the message itself...)

like image 841
Chris Avatar asked Aug 12 '12 11:08

Chris


1 Answers

Here is a solution that doesn't rely on macros. Instead, it uses a tiny bit of templating and lambda syntax.

template<typename Fn> 
void assert_fn( bool expr, Fn fn) {
  if (!expr) {
    fn();
    abort();
  }
}

The argument fn can be any callable.
For instance you can call it like so:

assert_fn( a==b, [&](){ cout << "Assertion failed: a="<< a << 
                                " is different from but b=" << b << endl; } ); 

The advantage is that the output is that you are not calling abort explicitly and the output is fully customizable. The this advantage, of course, are the seven extra characters of lambda function boilerplate: [&](){} )

like image 178
Mateo Avatar answered Sep 19 '22 07:09

Mateo