13

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...)

Community
  • 1
  • 1
Chris
  • 3,265
  • 5
  • 37
  • 50

5 Answers5

7

You could define your own macro

#define ASSERT_WITH_MSG(cond, msg) do \
{ if (!(cond)) { std::ostringstream str; str << msg; std::cerr << str.str(); std::abort(); } \
} while(0)
Greg
  • 1,660
  • 13
  • 12
6

It's relatively trivial to achieve this.

BOOST_ASSERT_MSG( length >= 0, (std::stringstream() << "No positive length found! It is " << length).str().c_str() )
Puppy
  • 144,682
  • 38
  • 256
  • 465
  • 4
    I hoped to do it that way, but the compiler complains: `error: ‘struct std::basic_ostream’ has no member named ‘str’` – Chris Aug 12 '12 at 14:13
  • 3
    Oh, yeah. I keep forgetting how broken the stringstream lib is. – Puppy Aug 12 '12 at 14:35
  • You have to `static_cast()` the return value of `operator<<()` to be able to use `std::stringstream::str()`. Otherwise you're trying to call `std::ostream::str()`, which doesn't exist. – Ruslan Apr 29 '16 at 10:48
3

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: [&](){} )

Mateo
  • 1,494
  • 1
  • 18
  • 27
1

I use the BOOST_ASSERT_MSG with my own wrapper around it, so that specifying the assert message with multiple operator<< seems less complex.

#if defined ASSERT_ENABLED 

    #define ASSERT(cond, msg) {\
        if(!(cond))\
        {\
            std::stringstream str;\
            str << msg;\
            BOOST_ASSERT_MSG(cond, str.str().c_str());\
        }\
    }
#else
    #define ASSERT(...) 
#endif

usage example, provide custom message like you are outputting to cout:

  ASSERT(execSize == (_oldSize - remaining), "execSize : " << execSize << ", _oldSize : " << _oldSize << ", remaining : " << remaining);

What it does is, if ASSERT_ENABLED is defined,enable the assertion messages. if(!(cond)) part is optimization, which avoids the costly string operations specified by macro parameter msg, if cond is true

rahul.deshmukhpatil
  • 977
  • 1
  • 16
  • 31
  • Do you really need `#if defined ASSERT_ENABLED` ? I think if assert is disabled, the statements are removed anyway, if using an optimising compiler. Am I correct? – Sohail Si Jun 21 '16 at 19:11
  • @SohailSi: Yes, `#if defined ASSERT_ENABLED` is for optimization. Generally release builds are meant to be with assertions disabled. This removes code from the binary. Smaller binary, less code, better use of instruction cache. While assert saves lot of time in debugging. Sometimes, a simple assert check could save 3 days of debugging time. – rahul.deshmukhpatil Jun 23 '16 at 07:28
  • I mean if defined ASSERT_ENABLED is false then the compiler should automatically remove the BOOST_ASSERT_MSG statements, hence should remove the rest of your assert code. So ASSERT_ENABLED is not necessary to be explicitly checked. But I am not sure why it is needed to be said explicitly. Where am I wrong? – Sohail Si Jun 23 '16 at 11:02
0

If you are working on Windows only, you can take a look to assert macro. Under the hood it uses _wassert. You can write your own assert macro using it. For instance in my case if I get some point, I want to show assert without conditions:

#ifdef DEBUG
    const std::wstring assert_msg = /* build the string here */;
    _wassert(assert_msg.c_str(), _CRT_WIDE(__FILE__), (unsigned)(__LINE__));
#endif

I think on other OS you can do the same trick, just take look at assert macro.

Vladislav
  • 394
  • 1
  • 14