Boost logo

Boost :

From: Gavin Collings (gcollings_at_[hidden])
Date: 2000-02-14 09:16:42


> > (R1) Give an error message *at the line* where the assertion
> > fails.
>
> Highly compiler-dependent! No single implementation is *ever* likely
to
> satisfy this requirement! Also, some people want readability...
> CodeWarrior's error messages are often so dismal that it can be very
hard to
> accomplish anything that looks reasonable in this regard. On the up
side,
> either class or function templates (I forget which) do give an
instantiation
> backtrace when they fail to compile.

Well, here's my effort to improve error messages, at least. It works
on the principle that a string is an invalid template argument, but can
be contained in a valid expression like charp < "literal" > integer
(looks like a template instantiation). With macros, an additional
benefit is that the boolean expression can be passed in as a string to
enhance the error message - which looks like: -

  "compile time assertion failure: file test.h line 373 (i > j)"

I've only tried it on gcc; any feedback on other platforms appreciated.

--
Gavin.
template <bool> struct ct_assert
{
   char * ct_message;
   int    val;
};
template <> struct ct_assert<false>
{
   template <typename> struct ct_message {};
};
#define require( B ) \
struct join( ct_check, __LINE__ ) : ct_assert<(B)>                   \
{                                                                    \
   void check()                                                      \
   {                                                                 \
      ct_message < "compile time assertion failure: file " __FILE__  \
                   " line " stringize( __LINE__ )                    \
                   " (" #B ")" > val;                                \
   }                                                                 \
}
#define do_join( X, Y ) X ## Y
#define join( X, Y ) do_join( X, Y )
#define do_stringize( X ) #X
#define stringize( X ) do_stringize( X )

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