Strategy
Strategy packages an algorithm behind a common interface so you can swap it at compile time or run time. In C++ that is a virtual compare, a template policy parameter, or a std::function / lambda you pass into std::sort.
Detailed Explanation
You sort users by name or by last login. The sort is the same; the comparison is not. The comparison is a strategy. std::sort's comparator is the standard library's version of the pattern.
Three C++ levels:
Runtime, virtual. struct Less { virtual bool operator()(const User&, const User&) const = 0; }; Store a unique_ptr<Less>. Good when the choice arrives from config or a plugin. Cost: a virtual call per comparison.
Runtime, std::function or a template parameter on the algorithm. sort(begin, end, [](...){...}) is already this. std::function<bool(const User&, const User&)> stores the strategy as a member when you cannot template the whole class.
Compile time, policy. template<class Compare> class Table. The strategy is a type. Zero overhead, different Table<Less> and Table<Greater> types. This is how std::set<T, Compare> works.
Pick the cheapest level that can still change when you need it to. Do not write a virtual strategy for a comparison you only decide at compile time.
Strategy versus if/else inside one function: once the branches grow their own helpers, a type or a callable per branch is clearer. Versus template method (base class calls a virtual hook): strategy has-a algorithm; template method is-a with a hole. Prefer strategy when the algorithm is reused on types that do not share a base.
A strategy should be stateless or obviously stateful. A comparator that mutates the elements is undefined behaviour inside sort. A strategy that holds a string filter is fine if you document it.
Ownership: if the context owns the strategy, unique_ptr<Strategy> or a value. If the caller owns it, a reference or function.
Code Examples
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
struct User { std::string name; int login; };
int main() {
std::vector<User> users{{"al", 2}, {"ada", 10}};
auto byName = [](const User& a, const User& b) { return a.name < b.name; };
std::sort(users.begin(), users.end(), byName);
std::cout << users[0].name << '\n'; // prints: ada
}#include <iostream>
#include <memory>
struct Greeter {
virtual ~Greeter() = default;
virtual void greet() const = 0;
};
struct Hi : Greeter {
void greet() const override { std::cout << "hi\n"; }
};
class App {
std::unique_ptr<Greeter> g_;
public:
explicit App(std::unique_ptr<Greeter> g) : g_(std::move(g)) {}
void run() const { g_->greet(); }
};
int main() {
App app(std::make_unique<Hi>());
app.run(); // prints: hi
}Common Gotchas
Stateful comparator in `sort`
must be a strict weak order and should not mutate elements. Treat it as `const`.
Policy templates change the type
you cannot put `Table<Less>` and `Table<Greater>` in one vector without a common runtime interface.
Virtual strategy on a hot `operator()`
measure. A lambda passed to `sort` inlines; a virtual compare will not.