考虑以下代码:

#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...);});  // ???
    };
};

有人可以帮我完成这个吗?还是完全想出一种新设计?

最佳答案

// 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()std::reference_wrapper,后者使用INVOKE并处理指向成员的指针和其他Callable对象。

10-06 07:31
查看更多