Boost logo

Boost :

Subject: [boost] [contract] static assertions
From: Lorenzo Caminiti (lorcaminiti_at_[hidden])
Date: 2010-05-13 15:56:33


Hello all,

Shall Boost.Contract static static assertions?

1) N1962 removed static assertions previously added by revisions N1866
and N1773 of the same proposal -- why?
2) In my experience, I never used static assertions within the contracts.
However, I am thinking to provide them because they might be useful to
better document contract interfaces with assertions that can be
efficiently evaluated at compile-time.

For example:

    template<typename T>
    struct x {

        CONTRACT_CLASS( (x) )

    public:

        CONTRACT_FUNCTION(
        (public) (void) (f)( (T)(val) )
            (precondition)(
                (static)(sizeof(T) <= sizeof(long)) // static
assertion (checked at compile-time)
                (val != 0) // run-time assertion
            )
         ({
             ...
         })

    };

This code will use one of the BOOST_MPL_ASSERT() to assert the static
condition in the precondition function generated by the
CONTRACT_FUNCTION() macro.

I think this is similar to asserting BOOST_MPL_ASSERT((sizeof(T) <=
sizeof(long))) in the function body or at class scope. However, having
the static assertion in the contracts has the following advantages:
(+) The static assertion compilation and check can be selectively
enabled/disabled when preconditions/postcondition/invariants are
turned on/off using CONTRACT_CHECK_PRECONDITON/etc.
(+) It more clearly documents that the (static) requirement `sizeof(T)
<= sizeof(long)` is added by the function f (instead of the class or
some other member function).

-- 
Lorenzo

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