boost::mpl根据条件延迟决定使用哪种类型

代码非常简单,原理都在注释里面。
CMakeLists.txt


cmake_minimum_required(VERSION 2.6)
project(lazy)

add_definitions(-std=c++14)

include_directories("/usr/local/include")
link_directories("/usr/local/lib")
file( GLOB APP_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
foreach( sourcefile ${APP_SOURCES} )
    file(RELATIVE_PATH filename ${CMAKE_CURRENT_SOURCE_DIR} ${sourcefile})
    string(REPLACE ".cpp" "" file ${filename})
    add_executable(${file} ${sourcefile})
    target_link_libraries(${file} boost_filesystem boost_thread boost_system boost_serialization pthread boost_chrono)
endforeach( sourcefile ${APP_SOURCES} )

main.cpp

#include <boost/mpl/apply.hpp>
#include <boost/mpl/eval_if.hpp>
#include <boost/mpl/identity.hpp>
#include <boost/type_traits/is_integral.hpp>
#include <boost/type_traits/make_unsigned.hpp>
#include <boost/static_assert.hpp>

using boost::mpl::_1;
using boost::mpl::_2;


// Condition 条件判断失败时,
// 生成的缺省类类型
struct fallback {};

// Cond(Param)判断成功时,使用Func(Param)返回的类型,
// 判断失败时,返回Fallback类型
template <class Func, class Param, class Cond, class Fallback=fallback>
struct apply_if {
    // 注意这里只是一个Cond类型,并没有取值,
    // 需要在eval_if_c里面去求值,所以需要在类型名前面加上 typename
    using condition_t = typename boost::mpl::apply<Cond, Param>::type;
    // 这里直接是apply,是一个结构体,所以不需要typename
    using applied_type = boost::mpl::apply<Func, Param>;

    // applied_type类型里面本身有一个 type类型定义,所以不需要用 boost::mpl::identity包裹,
    // 而Fallback里面不一定有type定义,所以需要包裹一下,加上type定义
    // identity类的定义如下,
    // BOOST_MPL_AUX_NA_PARAM(T) 这个宏的意思是 T = struct na类型
    // na是一个空的struct,有一个type定义
    // 如果默认不传的话, T就是na类型
    // 如果传了的话,例如此处,T就是fallback类型
     
    /**
     * 
     * template<
      typename BOOST_MPL_AUX_NA_PARAM(T)
        >
        struct identity
        {
            typedef T type;
            BOOST_MPL_AUX_LAMBDA_SUPPORT(1, identity, (T))
        };
    */
    using type = typename boost::mpl::eval_if_c<
        condition_t::value,
        applied_type,
        boost::mpl::identity<Fallback>
    >::type;
};

using res1_t = apply_if<
    boost::make_unsigned<_1>,
    int,
    boost::is_integral<_1>
    >::type;

BOOST_STATIC_ASSERT((boost::is_same<res1_t, unsigned int>::value));

using res2_t = apply_if<
    boost::make_unsigned<_1>,
    float,
    boost::is_integral<_1>
    >::type;

BOOST_STATIC_ASSERT((boost::is_same<res2_t, fallback>::value));



int main(int argc, char* argv[]) {

}

程序应该编译不报错就是对的。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值