gpt4 book ai didi

c++ - 处理嵌套 std::any_of 的模板函数

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:03:31 26 4
gpt4 key购买 nike

考虑这段代码:

#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)(...) 利用了 std::reference_wrapperoperator(),它使用 INVOKE 并处理指向成员的指针和其他 Callable 对象。

关于c++ - 处理嵌套 std::any_of 的模板函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30065703/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com