boost.BOOST_STATIC_ASSERT源码整理和使用说明

Source

#include <boost/config.hpp>

#include <boost/detail/workaround.hpp>

//

namespace kimi_boost

{

//这个模板类只有声明,没有定义

//注意:这里没有{}

//所以如果定义这个类型的对象将会失败

template <bool> struct STATIC_ASSERTION_FAILURE2;

//一个true类型的完全特化,并且带定义

template <> struct STATIC_ASSERTION_FAILURE2<true>{};

template<int x> struct static_assert_test2{};

#ifdef BOOST_STATIC_ASSERT

#undef BOOST_STATIC_ASSERT

#define BOOST_STATIC_ASSERT(B) typedef static_assert_test2< sizeof(STATIC_ASSERTION_FAILURE2< (bool)( B ) >) > nothing##__COUNTER__

#endif //BOOST_STATIC_ASSERT

}

Test code

void static_assert_test()

{

using namespace kimi_boost;

BOOST_STATIC_ASSERT(1);

BOOST_STATIC_ASSERT(1 == 1);

BOOST_STATIC_ASSERT(true);

BOOST_STATIC_ASSERT(0); //compile error

STATIC_ASSERTION_FAILURE2<true> asd;

STATIC_ASSERTION_FAILURE2<false> asd2;

}

Output

Compile time error

你可能感兴趣的:(static)