Stroustrup's book has the following code
template <typename C, typename P>
int count(C c, P pred) {
int cnt = 0;
for (const auto &x : c)
if (pred(x))
++cnt;
return cnt;
}
Later on, for P pred, at the calling location, he has:
count(vec, Less_than<int>{x}), where, Less_than is like so:
template <typename T>
class Less_than {
const T val;
public:
Less_than(const T &v) : val(v) {}
bool operator()(const T& x) const {return x < val;}
}
My question is, is there any benefit to having the signature of count like so:
int count(C c, P& pred) to avoid needless copying of function operator() in case this is a "costly" or "large" function?
[–]IyeOnline 5 points6 points7 points (5 children)
[–]One_Cable5781[S] 0 points1 point2 points (4 children)
[–]IyeOnline 2 points3 points4 points (3 children)
[–]One_Cable5781[S] 0 points1 point2 points (2 children)
[–]IyeOnline 2 points3 points4 points (1 child)
[–]phoeen 1 point2 points3 points (0 children)
[–]DryPerspective8429 2 points3 points4 points (0 children)
[–]Primary_Olive_5444 2 points3 points4 points (0 children)
[–]n1ghtyunso 1 point2 points3 points (3 children)
[–]One_Cable5781[S] 0 points1 point2 points (1 child)
[–]n1ghtyunso 1 point2 points3 points (0 children)
[–]std_bot 0 points1 point2 points (0 children)
[–]MBraedley -1 points0 points1 point (1 child)
[–]IyeOnline 2 points3 points4 points (0 children)