用于处理嵌套std :: any_of的模板函数

时间:2015-05-06 00:53:59

标签: c++ algorithm templates c++11

考虑以下代码:

#include <iostream>
#include <vector>
#include <algorithm>

struct Animal { virtual ~Animal() = default; };
struct Dog : Animal {};

struct Person {
    std::vector<struct Toddler*> children;
    const std::vector<Toddler*>& getChildren() const {return children;}
};

struct Toddler : Person {
    std::vector<Animal*> pets;
    const std::vector<Animal*>& getPets() const {return pets;}
};

int main() {
    Person* bob = new Person;
    Toddler* tom = new Toddler;
    tom->pets.push_back(new Dog);
    bob->children.push_back(tom);
    std::vector<Person*> people = {tom, bob};

    // Output: Does anybody in 'people' have a child that has a pet that is a dog?
    std::cout << std::boolalpha << std::any_of(people.begin(), people.end(),
        [](const Person* p) {return std::any_of(p->getChildren().begin(), p->getChildren().end(),
            [](const Toddler* t) {return
                std::any_of(t->getPets().begin(), t->getPets().end(),
                    [](const Animal* a) {return dynamic_cast<const Dog*>(a) != nullptr;});
            });
        }) << std::endl;  // true
}

我的目标是使用模板函数重写上面的输出

std::cout << anyOf (people, &Person::getChildren, &Toddler::getPets,
    [](const Animal* a) {return dynamic_cast<const Dog*>(a) != nullptr;}) << std::endl;

因此,只需要指定一个lambda函数,其他所有东西都更容易读写。以下是我到目前为止所做的事情,但这是非常错误的:

#include <type_traits>

template <typename...> struct AnyOf;

template <typename Predicate, typename... A>
bool anyOf (Predicate pred, const A&... a) {
    return AnyOf<Predicate, A...>::execute (pred, a...);
}

template <typename Predicate, typename Container>
struct AnyOf<Predicate, Container> {
    static bool execute (Predicate pred, const Container& c) {
        return std::any_of (c.begin(), c.end(), [pred](const typename Container::value_type& x) {return pred(x);});
    };
};

template <typename Predicate, typename Container, typename First, typename... Rest>
struct AnyOf<Predicate, Container, First, Rest...> : AnyOf<Predicate, typename std::result_of<First(void)>::type, Rest...> {
    using Base = AnyOf<Predicate, typename std::result_of<First(void)>::type, Rest...>;  // ???
    static bool execute (Predicate pred, const Container& c, First first, Rest... rest) {
        return std::any_of (c.begin(), c.end(), [=](const typename Container::value_type& x) {
            return Base::execute (pred, (x->*first)(), rest...);});  // ???
    };
};

有人可以帮我完成这个吗?或者想出一个全新的设计?

2 个答案:

答案 0 :(得分:1)

// Base case: only a predicate.
// Call std::any_of and we are done.

template<class Cont, class Pred>
bool anyOf(Cont&& c, Pred p) {
    using std::begin; using std::end; // enable ADL
    return std::any_of(begin(c), end(c), p);
}

// Recursive case: at least one Callable object to be applied to
// each element in the range.
// rest... contains both any remaining Callables and the predicate

template<class Cont, class F1, class... Rest>
bool anyOf(Cont&& c, F1 f, Rest... rest) {
    return anyOf(std::forward<Cont>(c), [&](auto&& a){ 
        return anyOf(std::ref(f)(std::forward<decltype(a)>(a)), rest...); 
    });
}

Demo

std::ref(f)(...)利用operator()的{​​{1}},std::reference_wrapper使用INVOKE并处理指向成员的指针和其他Callable对象。

答案 1 :(得分:0)

template <typename Predicate, typename Container, typename... MemberFunctions> struct AnyOf;

template <typename Predicate, typename... A>
bool anyOf (Predicate pred, const A&... a) {
    return AnyOf<Predicate, A...>::execute (pred, a...);
}

template <typename Predicate, typename Container, typename First, typename... Rest>
struct AnyOf<Predicate, Container, First, Rest...> {
    static bool execute (Predicate pred, const Container& container, First first, Rest... rest) {
        const auto element = (typename std::remove_pointer<typename Container::value_type>::type().*first)();
        using NextContainer = std::vector<typename decltype(element)::value_type>;
        NextContainer nextContainer;
        for (const typename Container::value_type& x : container)
            std::copy ((x->*first)().begin(), (x->*first)().end(), std::back_inserter(nextContainer));
        return AnyOf<Predicate, NextContainer, Rest...>::execute (pred, nextContainer, rest...);
    };
};

template <typename Predicate, typename Container>
struct AnyOf<Predicate, Container> {
    static bool execute (Predicate pred, const Container& c) {
        return std::any_of (c.begin(), c.end(), [pred](const typename Container::value_type& x) {return pred(x);});
    };
};

http://ideone.com/qWklc0

我欢迎更好的解决方案。