Boost logo

Boost :

From: Philippe Mori (philippe_mori_at_[hidden])
Date: 2005-07-04 16:55:22


> Even though I'm surely not the first one that came up to this, I've got a
> feature suggestion for the Static Assert library.
>
> It would probably be nice to have a variation of the macro that has a
second
> argument with a short description of the problem that caused the
assertion.
>
>
> For example:
>
> ...
> #elif defined(BOOST_MSVC)
> #define BOOST_STATIC_ASSERT2( B, explanation ) \
> typedef ::boost::static_assert_test<\
> sizeof(::boost::BOOST_JOIN(STATIC_ASSERTION_FAILURE__, explanation)<
(bool)(
> B ) >)>\
> BOOST_JOIN(boost_static_assert_typedef_, __COUNTER__)
> ...
>
> static_assert(2 > 3, two_not_larger_than_three);
>
> error C2039: 'STATIC_ASSERTION_FAILURE__two_not_larger_than_three' : is
not a
> member of 'boost'
>
>
>
> Comments?
>
>

Well, I tend to prefer a solution in the line of :

        STATIC_ASSERT_GT(2, 3);

My implementation is based on conversion on template classes :

        template <long> struct Value { };
        template <long p1, long p2, bool p3> struct ValueCond { };

        #define STATIC_ASSERT_EQUAL(a, b) \
                Value<(a)>(((void)0, Value<(b)>()))

        #define STATIC_ASSERT_GREATER(a, b) \
                ValueCond<(a), (b), ((a) > (b))> \
                (((void)0, ValueCond<(a), (b), true>()))

Well, this solution needs some improvments :

1) Ensure it works on most compiler. It works with Visual C++ 7.1 and
CodeWarrior for Palm 9.03.

2) Ensure that error message is meaningfull or try to find alternate
solution for that compiler.

3) Make it BOOST compliant

4) Make it works with larger integers. Part of the solution is using the
biggest integers availables at compile-time but it does still restrict the
range of checks (it won't works for very big unsigned numbers). It should be
done generally so that the user won't have to uses differents set of amcros
for big unsignbed number as this would require the user to know the type of
every constants he wants to check.

In practice, this is not often a big constraint as we can always uses
existing assertions for special cases and also because most check are
relatively small number (for example, checking the size of a buffer).

Philippe


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