1

使用 gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-44)/ boost 1.33.1 编译良好

#include <boost/mpl/assert.hpp>

int main()
{
    BOOST_MPL_ASSERT(( mpl::equal_to< mpl::long_<10>, mpl::int_<11> > ));
}

预处理后main()的样子:

int main()
{
    enum { mpl_assertion_in_line_5 = sizeof( boost::mpl::assertion_failed<false>( boost::mpl::assert_arg( (void (*) ( mpl::equal_to< mpl::long_<10>, mpl::int_<11> > ))0, 1 ) ) ) };
}

怎么了?

4

1 回答 1

3

您可能会丢失一些标题(这可能取决于 boost 的确切版本——我没有那个可用的标题)。这无法正确编译:

#include <boost/mpl/int.hpp>
#include <boost/mpl/long.hpp>
#include <boost/mpl/equal_to.hpp>
#include <boost/mpl/assert.hpp>

int main()
{
    BOOST_MPL_ASSERT(( boost::mpl::equal_to< boost::mpl::long_<10>, boost::mpl::int_<11> > ));
}

输出是:

# g++ -Wall t.cpp
t.cpp: In function ‘int main()’:
t.cpp:8:2: error: no matching function for call to ‘assertion_failed(mpl_::failed************ boost::mpl::equal_to<mpl_::long_<10l>, mpl_::int_<11> >::************)’

如果没有正确的头文件,就会发生其他各种不相关的编译错误。

如果您不想要命名空间限定符,您可以执行以下操作:

#include <boost/mpl/int.hpp>
#include <boost/mpl/long.hpp>
#include <boost/mpl/equal_to.hpp>
#include <boost/mpl/assert.hpp>

using namespace boost; // brings boost:: into scope

int main()
{
    BOOST_MPL_ASSERT(( mpl::equal_to< mpl::long_<10>, mpl::int_<11> > ));
}

甚至:

#include <boost/mpl/int.hpp>
#include <boost/mpl/long.hpp>
#include <boost/mpl/equal_to.hpp>
#include <boost/mpl/assert.hpp>

using namespace boost::mpl; // brings boost::mpl:: in scope

int main()
{
    BOOST_MPL_ASSERT(( equal_to< long_<10>, int_<11> > ));
}
于 2011-04-14T07:45:58.593 回答