snitch-org / snitch

Lightweight C++20 testing framework.
Boost Software License 1.0
257 stars 7 forks source link

Add a check macro that generates a compile-time error if the given expression is not `constexpr` #75

Open cschreib opened 1 year ago

cschreib commented 1 year ago

See https://github.com/cschreib/snitch/issues/60#issuecomment-1474863574.

Use case:

CHECK_IS_CONSTEXPR(constexpr_vector{10}.clear());

Issues:

Proposed implementation:

#define SNITCH_CHECK_IS_CONSTEXPR(...)                                                             \
    do {                                                                                           \
        auto& SNITCH_CURRENT_TEST = snitch::impl::get_current_test();                              \
        ++SNITCH_CURRENT_TEST.asserts;                                                             \
        SNITCH_WARNING_PUSH                                                                        \
        SNITCH_WARNING_DISABLE_PARENTHESES                                                         \
        SNITCH_WARNING_DISABLE_CONSTANT_COMPARISON                                                 \
        static_assert(((__VA_ARGS__), true));                                                      \
        SNITCH_WARNING_POP                                                                         \
    } while (0)

#define SNITCH_REQUIRE_IS_CONSTEXPR(...)                                                           \
    do {                                                                                           \
        auto& SNITCH_CURRENT_TEST = snitch::impl::get_current_test();                              \
        ++SNITCH_CURRENT_TEST.asserts;                                                             \
        SNITCH_WARNING_PUSH                                                                        \
        SNITCH_WARNING_DISABLE_PARENTHESES                                                         \
        SNITCH_WARNING_DISABLE_CONSTANT_COMPARISON                                                 \
        static_assert(((__VA_ARGS__), true));                                                      \
        SNITCH_WARNING_POP                                                                         \
    } while (0)