There is a simple POD data type like
struct Item {
int value1;
double value2;
bool value3;
}
Now I would like to write different count functions like it could be done with the following code (or some std method):
typedef bool Selector(const Item& i);
int count(const vector<Item>& items, Selector f) {
int sum = 0;
BOOST_FOREACH(const Item& i, items) {
if(f(i)) {
sum++;
}
}
return sum;
}
with f
e.g.
bool someSimpleSelector(const Item& i) {
return i.value1 > 0; // quite simple criterion
}
However in this approach the compiler can not inline the function call and thus will not inline my (trivial) selection code.
My question would be: is there a possibility to implement the above code in a way, where the compiler can inline my selection code, but without implementing the whole count function again and again explicitely (for example by using templates)?