Example (contrived):
I've written a method that takes can take a "filter" expression, and returns the number of matching items:
int getCount(std::function<bool(int, int)> filter)
{
// Iterate pairs of numbers p,q
// Count number of pairs where filter(p, q) is true
// Return count
}
I know I can invoke this as follows:
getCount([](int x, int y) { return x > y; });
But, since the intention is to write a filter "condition" or "expression" i.e. something very declarative and not imperative, I would ideally like to exclude the "return" statement.
Something along the lines of:
getCount([](int x, int y) { x > y; });
or
getCount([](int x, int y) { x > y });
Obviously the above aren't possible, but is there anything in for example std or boost that would allow me to achieve this intention?
Basically the user of the getCount method needs to be able to provide a filter condition only, without having to say "return" as an imperative statement.
You can use Boost.Phoenix for this:
using namespace boost::phoenix::placeholders;
getCount(_1 > _2);
_1
and _2
are argument placeholders and the whole relational expression forms a function object that returns the result of comparison.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments