Boost logo

Boost :

From: Philippe Mori (philippe_mori_at_[hidden])
Date: 2005-03-15 09:37:38


Well, the macro solution has the advantage that
the implementation can be customized for each
compiler to have the best possible error message
without too much noise (other errors or warnings).

What I like about boost solution is the fact that it can
be used almost everywhere (for ex. in a class
declaration or a global scope).

What I dislike is that the error message is longer than
necessary (with Visual C++) and that there are no
version that would allows to display numerical values
when doing comparison.

My preferred solution would also include macros
similar to that:

BOOST_STATIC_ASSERT_EQUAL(a, b)
BOOST_STATIC_ASSERT_LOWER_EQUAL(a, b)
BOOST_STATIC_ASSERT_GREATER_EQUAL(a, b)
BOOST_STATIC_ASSERT_NOT_EQUAL(a, b)
BOOST_STATIC_ASSERT_LOWER(a, b)
BOOST_STATIC_ASSERT_GREATER(a, b)

and the error message would typically display a and b.

The solution is based on weither convertion is possible :

    namespace boost {
        template <long A, long B, bool condition>
        struct value_condition { };
    }

As an example:

    #define BOOST_STATIC_ASSERT_LOWER(a, b) \
        boost::value_condition<(a), (b), ((a) < (b))> \
            (((void)0, boost::value_condition<(a), (b), true>()))

The version for BOOST_STATIC_ASSERT_EQUAL
could be simplified a bit since it will typically be used more
often:

    namespace boost {
        template <long V>
        struct value{ };
    }

    #define BOOST_STATIC_ASSERT_EQUAL(a, b) \
        boost::value<(a)>(((void)0, boost::value<(b)>()))

These macros could be modified a bit so that they would
works in any scope as BOOST_STATIC_ASSERT do.

The advantage is that the error message would should the
value as in:

    BOOST_STATIC_ASSERT_EQUAL(sizeof(int), 3);
    
An give an error message similar to:
    Cannot convert boost::value<3> to boost::value<4>

So if we makes the wrong guess, we knows the value
expected by the compiler...

I think it would be a nice addition to boost to have them.
Here I have used long (since it would works with negative
numbers) but it would be best if this can be modified to
works with any interger type. Maybe MPL could help for
that. If this not possible, we should uses the biggest signed
type on a platform or we might also have macros with an
additional argument.

Philippe


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