Proxy 4: The Next Leap in C++ Polymorphism
Proxy 4 is Microsoft's modern C++20 library that redefines runtime polymorphism without inheritance or virtual functions. It introduces skills, safer ownership patterns, and improved performance, making it easier than ever to build flexible, efficient systems in C++.
TL;DR
Proxy 4 is Microsoft's latest release of a header-only C++20 library that replaces inheritance-based polymorphism with a lightweight, flexible, and modular system. It introduces skills (pluggable capabilities), views for non-owning references, and shared proxies for efficient ownership. Think of it as polymorphism 2.0: easier, faster, and safer.
Who This Is For
- Beginner C++ developers gain a modern perspective on polymorphism beyond inheritance.
- Intermediate/advanced programmers discover how Proxy 4 can simplify design patterns and improve performance.
- Anyone curious about modern C++ practices and Microsoft's latest innovations.
Prerequisites: Familiarity with C++ basics (classes, objects, functions). No deep knowledge of polymorphism is required.
Introduction
In C++, polymorphism usually means inheritance + virtual functions. While powerful, this approach comes with overhead, rigid class hierarchies, and subtle pitfalls.
Microsoft's Proxy library takes a different approach. Instead of tying everything to a base class, Proxy lets you wrap any type and give it a shared interface no inheritance required. The result is cleaner code, faster execution, and more flexibility.
With Proxy 4, the library is now production-ready and introduces exciting new features like skills, weak proxies, and shared ownership.
Main Content
1. Getting Started with Proxy
Traditional C++ example:
struct Shape {
virtual void draw() = 0;
};
struct Circle : Shape {
void draw() override { std::cout << "Circle\n"; }
};
struct Square : Shape {
void draw() override { std::cout << "Square\n"; }
};
Here, you must define a base class (Shape
) and use virtual
functions.
With Proxy, no base class is needed:
#include <proxy/proxy.h>
struct Drawable : pro::facade_builder
::add_function<void()>("draw") // define capability
::build {};
void draw_circle() { std::cout << "Circle\n"; }
void draw_square() { std::cout << "Square\n"; }
int main() {
auto c= pro::make_proxy<Drawable>(&draw_circle);
auto s = pro::make_proxy<Drawable>(&draw_square);
c->draw();
s->draw();
}
👉 Any function or object can become a "shape" without inheritance.
2. The New Skills System
Skills are the highlight of Proxy 4. They're like Lego blocks of functionality you can attach to your proxy.
Some built-in skills:
skills::format
→ addsstd::format
support.skills::rtti
→ runtime type info + safe casting.skills::as_view
→ non-owning proxies.skills::slim
→ minimal, pointer-sized storage.
Example: add formatting support to any type.
struct Formattable : pro::facade_builder
::add_skill<pro::skills::format>
::build {};
int main() {
auto p = pro::make_proxy<Formattable>(123);
std::cout << std::format("Value = {}\n", *p); // prints: Value = 123
}
3. Ownership Made Safer
Proxy 4 introduces views and weak proxies:
proxy_view
→ borrow an object without owning it.weak_proxy
→ likestd::weak_ptr
, prevents dangling references.make_proxy_shared
→ lightweight shared ownership.
These tools give you explicit control over who owns what, reducing memory bugs.
4. Dispatch & Recursive Facades
- Dispatch improvements: clearer error messages, better handling of conversions.
- Recursive facades: allows proxies to return new proxies of the same type, useful for things like arithmetic chains or recursive operations.
5. Tooling & Docs
- New documentation site with examples and FAQs.
- Proxy is available directly on Compiler Explorer no installation needed, just try it online.
Key Takeaways
- Proxy replaces inheritance-based polymorphism with a modern, flexible system.
- Skills make it modular and composable.
- Views and weak proxies give safer ownership models.
- Performance is comparable (or better) than hand-written solutions.
- Production-ready Proxy has been used in Windows since 2022.
Resources and Links
Conclusion
Proxy 4 represents a new era for C++ polymorphism. By freeing developers from inheritance hierarchies and introducing composable skills, it makes code more flexible, maintainable, and performant.
If you're learning C++, Proxy is worth exploring early it may shape how you design systems in the years ahead.
Questions or feedback? Reach out at contact or connect on LinkedIn.
— Maruf