簡體   English   中英

模板函數處理嵌套的std :: any_of

[英]Template function to handle nested std::any_of

考慮以下代碼:

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

演示

std::ref(f)(...)利用std::reference_wrapperoperator() ,它使用INVOKE並處理指向成員的指針和其他Callable對象。

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

我歡迎更好的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM