Type erasure
Type erasure stores different types behind one named type by hiding the concrete type in a heap (or small-buffer) object with a virtual or function-pointer vtable. std::function, std::any, and std::shared_ptr<void> with a custom deleter are the standard erasers.
Detailed Explanation
A template is not a type you can put in a vector next to another instantiation. vector<Printer<A>> and vector<Printer<B>> are unrelated. Type erasure builds a single Printer that can hold either: internally a unique_ptr<Concept> and a Model<T> : Concept that forwards print to T. Callers see Printer. T is gone from the signature.
That is how std::function<void(int)> stores a lambda, a function pointer, or a functor. The callable's type is erased; the signature remains. That is how any stores an int or a string. That is how a plugin API takes a unique_ptr<Widget> without including the plugin's headers.
You write the pattern when you need a runtime collection of "things that can draw" and you do not want a shared base class in the concrete types (they may be from two libraries, or they may be values you do not want to inherit). If you own the types, a virtual Shape is simpler. If the set is closed and small, std::variant is simpler and does not allocate.
A minimal eraser:
struct Concept { virtual ~Concept() = default; virtual void call() = 0; };template<class T> struct Model : Concept { T t; void call() override { t.call(); } };struct Erased { unique_ptr<Concept> c; template<class T> Erased(T x) : c(make_unique<Model<T>>(Model<T>{std::move(x)})) {} };
Small-buffer optimisation (SBO) puts tiny Ts inside the eraser object, like function and any often do. Then a small lambda does not allocate.
Costs: allocation (unless SBO hits), an indirect call, and error messages that mention Model<lambda>. Benefits: a stable ABI, a single vector<Erased>, headers that do not include T.
std::function is not for hot inner loops. A template parameter inlines. Erase at the boundary (store the callback, register a plugin), not inside sort's comparator unless you must.
any erases without a known operation except "cast back". If you know the operation (draw), write a Concept with draw. Do not any plus any_cast on every call.
Code Examples
#include <functional>
#include <iostream>
#include <vector>
int main() {
std::vector<std::function<void()>> jobs;
jobs.emplace_back([] { std::cout << "a\n"; });
int n = 1;
jobs.emplace_back([n] { std::cout << n << '\n'; });
for (auto& j : jobs) j();
// prints:
// a
// 1
}#include <iostream>
#include <memory>
#include <utility>
struct Concept {
virtual ~Concept() = default;
virtual void run() const = 0;
};
template <class T>
struct Model : Concept {
T t;
explicit Model(T x) : t(std::move(x)) {}
void run() const override { t(); }
};
class Job {
std::unique_ptr<Concept> c_;
public:
template <class T>
explicit Job(T x) : c_(std::make_unique<Model<T>>(std::move(x))) {}
void run() const { c_->run(); }
};
int main() {
Job j([] { std::cout << "go\n"; });
j.run(); // prints: go
}Common Gotchas
Erased object whose `T` captured dangling references
the eraser keeps `T` alive, not `T`'s referents. Same lambda-capture rules.
`variant` is not erasure
the set of types is in the type. Use it when the set is closed.
`any` plus frequent `any_cast`
you wanted a `Concept` with the methods you actually call.