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

Boost Static Assert详解
本文介绍了一个使用Boost库实现静态断言的方法。通过定义一个未具化的模板类,并对其真值特例进行定义,实现了编译期检查。文章展示了如何自定义Boost静态断言宏,并通过示例代码解释了其工作原理。

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

以下是find boost的cmakelist代码,我已经安装好了1.74版本的boost需要更改哪些地方 # Find boost # To change the path for boost, you will need to set: # BOOST_ROOT: path to install prefix for boost # Boost_NO_SYSTEM_PATHS: set to true to keep the find script from ignoring BOOST_ROOT if(MSVC) # By default, boost only builds static libraries on windows set(Boost_USE_STATIC_LIBS ON) # only find static libs # If we ever reset above on windows and, ... # If we use Boost shared libs, disable auto linking. # Some libraries, at least Boost Program Options, rely on this to export DLL symbols. if(NOT Boost_USE_STATIC_LIBS) list(APPEND GTSAM_COMPILE_DEFINITIONS_PUBLIC BOOST_ALL_NO_LIB BOOST_ALL_DYN_LINK) endif() # Virtual memory range for PCH exceeded on VS2015 if(MSVC_VERSION LESS 1910) # older than VS2017 list(APPEND GTSAM_COMPILE_OPTIONS_PRIVATE -Zm295) endif() endif() # If building DLLs in MSVC, we need to avoid EIGEN_STATIC_ASSERT() # or explicit instantiation will generate build errors. # See: https://bitbucket.org/gtborg/gtsam/issues/417/fail-to-build-on-msvc-2017 # if(MSVC AND BUILD_SHARED_LIBS) list(APPEND GTSAM_COMPILE_DEFINITIONS_PUBLIC EIGEN_NO_STATIC_ASSERT) endif() # Store these in variables so they are automatically replicated in GTSAMConfig.cmake and such. set(BOOST_FIND_MINIMUM_VERSION 1.43) #before1.43 set(BOOST_FIND_MINIMUM_COMPONENTS serialization system filesystem thread program_options date_time timer chrono regex) find_package(Boost ${BOOST_FIND_MINIMUM_VERSION} COMPONENTS ${BOOST_FIND_MINIMUM_COMPONENTS}) # Required components if(NOT Boost_SERIALIZATION_LIBRARY OR NOT Boost_SYSTEM_LIBRARY OR NOT Boost_FILESYSTEM_LIBRARY OR NOT Boost_THREAD_LIBRARY OR NOT Boost_DATE_TIME_LIBRARY) message(FATAL_ERROR "Missing required Boost components >= v1.43, please install/upgrade Boost or configure your search paths.") endif() # Allow for not using the timer libraries on boost < 1.48 (GTSAM timing code falls back to old timer library) option(GTSAM_DISABLE_NEW_TIMERS "Disables using Boost.chrono for timing" OFF) # JLBC: This was once updated to target-based names (Boost::xxx), but it caused # problems with Boost versions newer than FindBoost.cmake was prepared to handle, # so we downgraded this to classic filenames-based variables, and manually adding # the target_include_directories(xxx ${Boost_INCLUDE_DIR}) set(GTSAM_BOOST_LIBRARIES optimized ${Boost_SERIALIZATION_LIBRARY_RELEASE} ${Boost_SYSTEM_LIBRARY_RELEASE} ${Boost_FILESYSTEM_LIBRARY_RELEASE} ${Boost_THREAD_LIBRARY_RELEASE} ${Boost_DATE_TIME_LIBRARY_RELEASE} ${Boost_REGEX_LIBRARY_RELEASE} debug ${Boost_SERIALIZATION_LIBRARY_DEBUG} ${Boost_SYSTEM_LIBRARY_DEBUG} ${Boost_FILESYSTEM_LIBRARY_DEBUG} ${Boost_THREAD_LIBRARY_DEBUG} ${Boost_DATE_TIME_LIBRARY_DEBUG} ${Boost_REGEX_LIBRARY_DEBUG} ) message(STATUS "GTSAM_BOOST_LIBRARIES: ${GTSAM_BOOST_LIBRARIES}") if (GTSAM_DISABLE_NEW_TIMERS) message("WARNING: GTSAM timing instrumentation manually disabled") list(APPEND GTSAM_COMPILE_DEFINITIONS_PUBLIC DGTSAM_DISABLE_NEW_TIMERS) else() if(Boost_TIMER_LIBRARY) list(APPEND GTSAM_BOOST_LIBRARIES optimized ${Boost_TIMER_LIBRARY_RELEASE} ${Boost_CHRONO_LIBRARY_RELEASE} # debug # ${Boost_TIMER_LIBRARY_DEBUG} # ${Boost_CHRONO_LIBRARY_DEBUG} ) else() list(APPEND GTSAM_BOOST_LIBRARIES rt) # When using the header-only boost timer library, need -lrt message("WARNING: GTSAM timing instrumentation will use the older, less accurate, Boost timer library because boost older than 1.48 was found.") endif() endif() if(NOT (${Boost_VERSION} LESS 105600)) message("Ignoring Boost restriction on optional lvalue assignment from rvalues") list(APPEND GTSAM_COMPILE_DEFINITIONS_PUBLIC BOOST_OPTIONAL_ALLOW_BINDING_TO_RVALUES BOOST_OPTIONAL_CONFIG_ALLOW_BINDING_TO_RVALUES) endif()
08-01
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值