用消息提升static_assert?

时间:2010-07-13 21:36:50

标签: c++ debugging boost metaprogramming static-assert

在1.43上提升似乎BOOST_STATIC_ASSERT只允许放置一个布尔值,是否有一些替代方法允许我在编译错误时显示消息?

3 个答案:

答案 0 :(得分:5)

MPL有BOOST_MPL_ASSERT_MSG。例如。使用GCC 4.2。有了这个:

BOOST_MPL_ASSERT_MSG(false, THIS_DOESNT_WORK, (void));

...导致:

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

答案 1 :(得分:3)

你有没有试过像:

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

如果编译器支持C ++ 0x static_assert,则可以执行以下操作:

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

答案 2 :(得分:0)

提升1.47及以后支持BOOST_STATIC_ASSERT_MSG。用法:

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

如果C ++ 11可用,或者编译器支持static_assert(),则错误消息将为msg字符串。否则宏被视为BOOST_STATIC_ASSERT(condition)