I am working on a problem in C ++ that involves many subset and conversion operations for large amounts of data. To this end, I created a map function and a sort of list comprehension. I found that the bunch of predicates that I write also has inversions, so I need to write:
template <typename type_t>
bool HasTenFoo(const type_t &t) {
return t.foo >= 10.0;
}
and
template <typename type_t>
bool DoesntHaveTenFoo(const type_t &t) {
return t.foo < 10.0;
}
None of them are real examples, but they are representative. I also use a large number of functors, such as:
class HasEnoughFoo {
public:
HasEnoughFoo (double bar) { this->bar = bar; }
template<typename type_t>
bool operator()(const type_t &t) const { return t.foo >= bar; }
private:
double bar;
};
some of them must also have inverse. Instead of duplicating the code unnecessarily, I would like to write a functor that takes a predicate as an argument and returns (value) the opposite of that predicate. My fist, one below:
template<typename predicate_t>
class Not {
public:
template <typename predicate_t>
Not(predicate_t *p) { predicate = p; }
template <typename type_t>
bool operator()(const type_t &t) const {
return !(*predicate)(t);
}
private:
predicate_t *predicate;
};
I would call it something like:
new_list = old_list.subset(Not<HasEnoughFoo>(&HasEnoughFoo(10.0));
or
new_list = old_list.subset(Not<HasTenFoo>(&HasTenFoo));
, , predicate_t HasEnoughFoo, , predicate_t HasTenFoo.
Visual Studio , 'HasTenFoo' is not a valid template type argument for parameter 'predicate_t'. Not(), , ?