Boost库是C++的一个非常强大的开源库,它提供了众多的模板元编程工具和数据结构,能够支持泛型编程和元编程。其中,mpl模块更是提供了丰富的元编程工具,可以很方便地实现各种类型转换、操作和计算等。
本篇文章将着重介绍mpl模块中的count_if算法实现,并给出相应的测试程序。
count_if算法的作用是对输入序列中满足特定条件的元素进行计数,并返回计数结果。在mpl模块中,它的实现主要使用递归和模板特化的方式,代码如下:
template <typename Sequence, typename Pred>
struct count_if_impl
{
typedef typename begin<Sequence>::type first;
typedef typename end<Sequence>::type last;
typedef typename while_<
not_< is_same<first, last> >,
typename if_<
typename apply<Pred, typename deref<first>::type>::type,
typename next<first>::type,
first
>::type
>::type iter;
BOOST_STATIC_CONSTANT(
long, value = ( distance< typename
订阅专栏 解锁全文
948



被折叠的 条评论
为什么被折叠?



