1

考虑以下对 Boost.MPL 风格的元编程版本的尝试std::any_of

    #include <iostream>                     // cout
    #include <type_traits>                  // is_base_of, is_pod
    #include <boost/mpl/apply.hpp>          // apply
    #include <boost/mpl/fold.hpp>           // fold
    #include <boost/mpl/lambda.hpp>         // lambda, _1, _2
    #include <boost/mpl/logical.hpp>        // and_, true_   
    #include <boost/mpl/vector.hpp>         // vector

    template
    <
            typename Sequence,
            typename Pred
    >
    struct all_of
    :
            boost::mpl::fold<
                    Sequence,
                    boost::mpl::true_,
                    boost::mpl::lambda<
                            boost::mpl::and_<
                                    boost::mpl::_1,
                                    boost::mpl::apply< Pred, boost::mpl::_2 >
                            >
                    >
            >
    {};

    typedef int P1; typedef char P2; typedef float P3;

    typedef boost::mpl::vector<
            P1, P2, P3
    > pod_types;

    struct B {}; struct D1: B {}; struct D2: B {}; struct D3: B {};

    typedef boost::mpl::vector<
            D1, D2, D3
    > derived_types;

    int main() 
    {
            std::cout << (std::is_pod<P1>::value) << '\n';  // true
            std::cout << (std::is_pod<P2>::value) << '\n';  // true
            std::cout << (std::is_pod<P3>::value) << '\n';  // true       

            std::cout << (
                    all_of<
                            pod_types, 
                            std::is_pod< boost::mpl::_1 >                        
                    >::type::value  // true
            ) << '\n';

            std::cout << (std::is_base_of<B, D1>::value) << '\n';   // true
            std::cout << (std::is_base_of<B, D2>::value) << '\n';   // true
            std::cout << (std::is_base_of<B, D3>::value) << '\n';   // true

            std::cout << (
                    all_of<
                            derived_types, 
                            std::is_base_of< B, boost::mpl::_1 >    
                    >::type::value  // false (but should be true)
            ) << '\n';

            return 0;
    }

这将打印出:1 1 1 1 1 1 1 0。即,作为谓词传递的最终调用all_ofwith生成 false。std::is_base_of为什么这不起作用?显然,基类B没有正确绑定到谓词。我应该如何传递二元谓词?mpl::lambda 或 mpl::bind 的某种组合?

更新

基于 Luc Touraille 的出色回答,这里是我的问题的无 lambda 解决方案,另外还有编译时版本none_ofany_of

    template<typename Sequence, typename Pred>
    struct all_of
    :
            std::is_same< typename 
                    boost::mpl::find_if<
                            Sequence,
                            boost::mpl::not_<Pred>
                    >::type, typename 
                    boost::mpl::end<Sequence>::type
            >
    {};

    template<typename Sequence, typename Pred>
    struct none_of
    :
            all_of< Sequence, boost::mpl::not_< Pred > >
    {};

    template<typename Sequence, typename Pred>
    struct any_of
    :
            boost::mpl::not_< none_of< Sequence, Pred > >
    {};
4

2 回答 2

1

您需要将谓词转换为 lambda,否则 _1 将被解释为折叠的第一级调用,而不是传递给 Pred 的第一个参数。mpl::lambda 是你需要的

于 2012-02-19T19:21:00.967 回答
1

这是使用find_if代替的解决方案fold

#include <type_traits>
#include <boost/mpl/end.hpp>
#include <boost/mpl/find_if.hpp>
#include <boost/mpl/logical.hpp>

template
<
    typename Sequence,
    typename Pred
>
struct all_of
:
    std::is_same< typename
        boost::mpl::end< Sequence >::type, typename
        boost::mpl::find_if<
            Sequence,
            boost::mpl::not_< Pred >
        >::type
    >
{};

如果你想坚持fold,你需要lambda在调用它之前将谓词转换为元函数,以保护参数:

boost::mpl::apply< typename
    boost::mpl::lambda< Pred >::type,
    boost::mpl::_2
>

但是,您会注意到这也不起作用。我不确定为什么,我认为这与Boost mailing list 上的讨论有关。显然,存在一个问题,其 arityapply大于lambda. 无论如何,一个简单的解决方法是使用apply1而不是apply. 这是完整的解决方案:

template
<
        typename Sequence,
        typename Pred
>
struct all_of
  : boost::mpl::fold<
        Sequence,
        boost::mpl::true_,
        boost::mpl::and_<
            boost::mpl::_1,
            boost::mpl::apply1< typename
                boost::mpl::lambda< Pred >::type,
                boost::mpl::_2
            >
        >
    >
{};
于 2012-02-22T09:41:12.267 回答