Boost logo

Boost :

From: Vesa Karvonen (vesa.karvonen_at_[hidden])
Date: 2001-11-24 08:51:20


From: "Andrei Alexandrescu" <andrewalex_at_[hidden]>
> Is there a chance of adding a loki-like macro to static_assert.hpp that
> would allow a user-defined message to be (likely) included in the error
> message?
>
> The idea is to allow the user to specify an error message in the form of an
> identifier.
>
> #define STATIC_ASSERT_MSG(b, msg) \
> { \
      ^
> struct msg \
> { \
> msg(::boost::loki::int2type<true>) {} \
> } msg(::boost::loki::int2type<(b) != 0>()); \
> }
      ^
The above STATIC_ASSERT_MSG macro can only be used in function scope which
highly limits its use. IMHO, an industry strength STATIC_CHECK must be usable
at:
- namespace,
- class, and
- function scope.

What I mean is that you can not write something like this using the above
STATIC_ASSERT_MSG:

    template<int x, int must_be_less_than_x>
    struct some_template
    {
        STATIC_ASSERT_MSG(must_be_less_than_x < x, Bad_Parameters_Buddy);

        // ...
    };

Furthermore, if you want to make the above STATIC_ASSERT_MSG safe to use, you
should use construct that can not be accidentally used like this:

    if (expr_1)
        if (expr_2)
            STATIC_CHECK(const_expr);
        else
            stmt_1();

When will stmt_1() be executed?

So, use "do {...} while (0)" instead of "{ ... }".

> Then if you say:
>
> STATIC_ASSERT_MSG(false, This_Will_Appear_In_The_Error_Message);
>
> then it's very likely you will see the actual string
> This_Will_Appear_In_The_Error_Message in the compiler output. My compiler
> says:

IMHO it is not imperative to have the compiler error message readable, beyond
indicating that it is an assertion, because on an interactive programming
environment, you can typically jump to the point of the error with a maximum
of one keystroke, where you can read the complete static assertion with just
the amount of nicely formatted comments that you like:

    STATIC_ASSERT(expression); // nicely formatted comment, maybe ASCII art


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