【发布时间】:2010-09-16 15:31:50
【问题描述】:
我记得BOOST_MPL_ASSERT 曾经是首选。这仍然是真的吗?有人知道为什么吗?
【问题讨论】:
我记得BOOST_MPL_ASSERT 曾经是首选。这仍然是真的吗?有人知道为什么吗?
【问题讨论】:
[回答我自己的问题]
这取决于。这是一个苹果与橘子的比较。尽管相似,但这些宏不可互换。以下是每个工作原理的摘要:
如果P != true,BOOST_STATIC_ASSERT( P ) 会产生编译错误。
BOOST_MPL_ASSERT(( P )) 如果P::type::value != true 会产生编译错误。
尽管需要双括号,后一种形式特别有用,因为它可以生成更多信息性错误消息如果使用布尔空元元函数来自 Boost.MPL 或 TR1 的 <type_traits> 作为谓词。
这是一个示例程序,演示了如何使用(和误用)这些宏:
#include <boost/static_assert.hpp>
#include <boost/mpl/assert.hpp>
#include <type_traits>
using namespace ::boost::mpl;
using namespace ::std::tr1;
struct A {};
struct Z {};
int main() {
// boolean predicates
BOOST_STATIC_ASSERT( true ); // OK
BOOST_STATIC_ASSERT( false ); // assert
// BOOST_MPL_ASSERT( false ); // syntax error!
// BOOST_MPL_ASSERT(( false )); // syntax error!
BOOST_MPL_ASSERT(( bool_< true > )); // OK
BOOST_MPL_ASSERT(( bool_< false > )); // assert
// metafunction predicates
BOOST_STATIC_ASSERT(( is_same< A, A >::type::value ));// OK
BOOST_STATIC_ASSERT(( is_same< A, Z >::type::value ));// assert, line 19
BOOST_MPL_ASSERT(( is_same< A, A > )); // OK
BOOST_MPL_ASSERT(( is_same< A, Z > )); // assert, line 21
return 0;
}
为了比较,以下是我的编译器 (Microsoft Visual C++ 2008) 为上面的第 19 行和第 21 行生成的错误消息:
1>static_assert.cpp(19) : error C2027: use of undefined type 'boost::STATIC_ASSERTION_FAILURE<x>'
1> with
1> [
1> x=false
1> ]
1>static_assert.cpp(21) : error C2664: 'boost::mpl::assertion_failed' : cannot convert parameter 1 from 'boost::mpl::failed ************std::tr1::is_same<_Ty1,_Ty2>::* ***********' to 'boost::mpl::assert<false>::type'
1> with
1> [
1> _Ty1=A,
1> _Ty2=Z
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
因此,如果您使用元函数(定义为 here)作为谓词,那么 BOOST_MPL_ASSERT 的代码既不冗长,又在断言时提供更多信息。
对于简单的布尔谓词,BOOST_STATIC_ASSERT 的代码不太冗长,尽管其错误消息可能不太清楚(取决于您的编译器。)
【讨论】:
BOOST_MPL_ASSERT 通常(仍然)被认为更好。来自它的消息更容易查看(如果您使用BOOST_MPL_ASSERT_MSG,则更容易理解)。几个月前有一些关于弃用BOOST_STATIC_ASSERT 的讨论,尽管我认为每个人最终都同意世界上仍有它的空间。
【讨论】: