Raise static_assert with message?

in 1.43 boost it seems that BOOST_STATIC_ASSERT just allows you to set a boolean, is there some kind of alternative that allows me to display a message also with a compilation error?

+3
source share
3 answers

MPL has BOOST_MPL_ASSERT_MSG. For instance. using GCC 4.2. with this:

BOOST_MPL_ASSERT_MSG(false, THIS_DOESNT_WORK, (void));

... leads to:

/path/to/file.cpp:42: error: no matching function for call to 
'assertion_failed(mpl_::failed************ (function()::THIS_DOESNT_WORK::************)())'
+5
source

Have you tried something like:

BOOST_STATIC_ASSERT(sizeof(long) == 64 && "Must have 64-bit long!")

If your compiler supports C ++ 0x static_assert, you can do:

static_assert(sizeof(long) == 64, "Must have 64-bit long!")
+3
source

Boost 1.47 BOOST_STATIC_ASSERT_MSG. :

#include <boost/static_assert.hpp>
BOOST_STATIC_ASSERT_MSG(condition, msg)

If C ++ 11 is available or the compiler supports it static_assert(), the error message will be msgstring. Otherwise, the macro is considered asBOOST_STATIC_ASSERT(condition)

0
source

Source: https://habr.com/ru/post/1754508/


All Articles