Boost logo

Boost :

Subject: [boost] [assert] static_assert envy
From: Beman Dawes (bdawes_at_[hidden])
Date: 2011-01-17 10:06:53


Whenever I write a BOOST_ASSERT macro, I'm envious of C++0x static_assert
because of its ability to supply a clear error message. I'd really like to
be able to write a variant of BOOST_ASSERT like this:

#include <boost/assert.hpp>
int main()
{
  BOOST_EXTENDED_ASSERT(true, "This will not assert");
  BOOST_EXTENDED_ASSERT(false, "This will assert");
  return 0;
}

And then get debug mode output like this:

***** Internal Program Error - Assertion Failed *****
extended_assert_test.cpp(6): error in int main(void): This will assert

Adding the code below at the end of <boost/assert.hpp> will do just that. It
isn't totally ready for prime time, but it would be nice to find out if
others would like something similar added to<boost/assert.hpp>.

--Beman

// experimental code

#ifndef NDEBUG

# include <iostream>
# include <cstdlib>
# include <boost/current_function.hpp>

  namespace boost { namespace assertion { namespace detail {
  inline void extended_assert_failed(char const * msg, char const *
function,
    char const * file, long line)
  {
    std::cerr << "***** Internal Program Error - Assertion Failed *****\n";
    std::cerr << file << '(' << line << "): error in " << function << ": "
<< msg << std::endl;
    std::exit(99999);
  }
  }}} // boost::assertion::detail

# undef BOOST_EXTENDED_ASSERT

# define BOOST_EXTENDED_ASSERT(expr, msg) \
    ((expr)? ((void)0): ::boost::assertion::detail::extended_assert_failed\
      (msg, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__))
#else
# define BOOST_EXTENDED_ASSERT(expr, msg)
#endif


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk