Reports ask questions about a batch: how many events are adjustments, how many touch item 2, how many would remove stock. The loop is the same each time; only the condition changes. You could express the condition as a virtual Predicate interface, as Day 2 did for rules, but that means a class per question, a heap allocation per object, and an indirect call per element, all to pass a condition the caller knows when the code is compiled.
A function template takes the condition's type as a parameter. The caller passes a lambda, the compiler deduces its type, and it generates a version of the loop for that exact type, where the call can be inlined. Day 2's virtual rules are chosen from configuration at run time; report predicates are chosen in the source. Different problems, different tools, and the engine uses both.
Predict and run
#include <iostream>
#include <vector>
template<class Predicate>
std::size_t count_matching(const std::vector<int>& values, Predicate matches) {
std::size_t count = 0;
for (int value : values) { if (matches(value)) { ++count; } }
return count;
}
int main() {
std::vector<int> values{1,2,3};
std::cout << count_matching(values,[](int n) { return n > 1; }) << "\n";
return 0;
}Check your prediction
2You never wrote Predicate's type. The compiler deduced it from the argument, the unnamed type of that lambda, and instantiated count_matching for it. Try [](int) { return true; } and [](int) { return false; }: they give 3 and 0, and each is a separate instantiation of the same loop. The loop, the counter and the return type stay common; only the call matches(value) differs.
Every lambda has its own type
A lambda expression creates an object of a unique, unnamed closure type. Two lambdas with identical text still have different types:
#include <iostream>
#include <type_traits>
int main() {
auto first = [](int n) { return n > 1; };
auto second = [](int n) { return n > 1; };
auto copy = first;
std::cout << std::is_same_v<decltype(first), decltype(second)> << " "
<< std::is_same_v<decltype(first), decltype(copy)> << "\n";
return 0;
}0 1That is why you cannot name a lambda's type and must let the compiler deduce it, with auto for variables and a template parameter for functions. It is also why templates are fast here: the compiler knows exactly which function body matches(value) calls. A template parameter accepts anything that can be called with an int, including a named struct with operator() and a plain function pointer, as the practice shows.
Captured state and pass by value
A lambda that captures a value carries it in its closure object. Capturing by value is how you build a parameterized condition without a class:
#include <iostream>
#include <vector>
template<class Predicate>
std::size_t count_matching(const std::vector<int>& values, Predicate matches) {
std::size_t count = 0;
for (int value : values) { if (matches(value)) { ++count; } }
return count;
}
int main() {
std::vector<int> deltas{-3, 5, -20, 2, -1};
for (int limit : {0, -2, -10}) {
auto below = [limit](int delta) { return delta < limit; };
std::cout << "below " << limit << ": " << count_matching(deltas, below) << "\n";
}
return 0;
}below 0: 3
below -2: 2
below -10: 1count_matching takes Predicate matches by value, which copies the callable. For a stateless lambda or a captured threshold that copy is free and harmless. For a callable that records something, it is a trap, because the template updates its own copy:
#include <functional>
#include <iostream>
#include <vector>
template<class Predicate>
std::size_t count_matching(const std::vector<int>& values, Predicate matches) {
std::size_t count = 0;
for (int value : values) { if (matches(value)) { ++count; } }
return count;
}
struct Probe {
int calls = 0;
bool operator()(int value) { ++calls; return value > 0; }
};
int main() {
std::vector<int> values{1, -2, 3};
Probe probe;
std::cout << count_matching(values, probe) << " matches, " << probe.calls << " calls seen\n";
std::cout << count_matching(values, std::ref(probe)) << " matches, " << probe.calls << " calls seen\n";
return 0;
}2 matches, 0 calls seen
2 matches, 3 calls seenThe first call counted calls in a copy that died when the function returned. std::ref(probe) passes a std::reference_wrapper<Probe>, which is itself callable and forwards each call to the original. The project's report predicates avoid the issue by being stateless and read-only, which is the better design: a predicate answers a question and changes nothing.
The costs of templates
Templates are not free. The definition must be visible wherever it is used, so count_events lives in engine.h rather than engine.cpp. Each distinct callable type produces another copy of the function in the binary. And when a caller passes something unsuitable, the error appears inside the template body, at matches(value), often far from the caller's mistake. Pass a lambda that takes const std::string& to count_matching and the compiler reports that no matching function call exists inside the loop, with notes leading back to main. The next lesson moves that requirement onto the declaration with a concept, so the error names the real problem at the call site.
Practice
Your turn
Write count_matching as above, then call it three times on {1, 2, 4, 7, 10} with three kinds of callable: a lambda selecting even values, a named struct AtLeast holding a limit of 4, and a pointer to a plain function bool is_odd(int). Also count an empty vector with an always-true lambda. Print:
even: 3
at least 4: 3
odd: 2
empty: 0#include <iostream>
#include <vector>
int main() {
std::vector<int> values{1, 2, 4, 7, 10};
// Write count_matching and call it with three kinds of callable.
return 0;
}Hint 1: the idea
One template accepts all three, because each can be called as matches(value). The compiler deduces a different Predicate for each call.
Hint 2: a first step
Define struct AtLeast { int limit; bool operator()(int value) const { return value >= limit; } }; and pass AtLeast{4}. Pass the function as is_odd; it decays to a function pointer.
Show one solution
#include <iostream>
#include <vector>
template<class Predicate>
std::size_t count_matching(const std::vector<int>& values, Predicate matches) {
std::size_t count = 0;
for (int value : values) { if (matches(value)) { ++count; } }
return count;
}
struct AtLeast {
int limit;
bool operator()(int value) const { return value >= limit; }
};
bool is_odd(int value) { return value % 2 != 0; }
int main() {
std::vector<int> values{1, 2, 4, 7, 10};
std::cout << "even: " << count_matching(values, [](int n) { return n % 2 == 0; }) << "\n";
std::cout << "at least 4: " << count_matching(values, AtLeast{4}) << "\n";
std::cout << "odd: " << count_matching(values, is_odd) << "\n";
std::cout << "empty: " << count_matching({}, [](int) { return true; }) << "\n";
return 0;
}even: 3
at least 4: 3
odd: 2
empty: 0The template deduced a closure type, AtLeast and bool (*)(int) in turn. The {} call works because the first parameter is a fixed const std::vector<int>&, so the braces become an empty vector; only the predicate's type is deduced. is_odd uses != 0 rather than == 1 on purpose: -3 % 2 is -1 in C++, so == 1 would miss negative odd numbers. Next you state what a predicate must be, so that wrong callables fail at the call.
Completion is saved in this browser only. It does not measure mastery.