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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值