xiaohongshu
xiaohongshu
alexandrescu-modern-cpp-design
Write C++ code following Andrei Alexandrescu's Modern C++ Design principles. Emphasizes policy-based design, template metaprogramming, and type-safe generic abstractions. Use when designing flexible, reusable libraries or when compile-time computation beats runtime overhead.
Full skill instructions
Andrei Alexandrescu's "Modern C++ Design" revolutionized how we think about C++ templates. His work on Loki library and policy-based design showed that templates are not just for containers—they're a compile-time programming language.
"C++ templates are Turing-complete. Use this power wisely."
"Policy-based design: assemble types from interchangeable parts."
Alexandrescu believes in pushing computation to compile time and using the type system as a design tool, not just a safety mechanism.
Policy-Based Design: Build classes from interchangeable policy classes that customize behavior without inheritance overhead.
Compile-Time over Runtime: What can be computed at compile time should be.
Type Lists and Metaprogramming: Types themselves become first-class citizens that can be manipulated.
Design Patterns in Types: Classic GoF patterns implemented with zero runtime overhead.
static_assert to document and enforce requirementsconstexpr functions over template metafunctions (modern C++)// Traditional OOP: Runtime overhead, fixed at compile time anyway
class Widget : public ICreationPolicy, public IThreadingPolicy { /* ... */ };
// Policy-Based: Zero overhead, infinitely configurable
template <
class CreationPolicy,
class ThreadingPolicy = SingleThreaded,
class CheckingPolicy = NoChecking
>
class SmartPtr : public CreationPolicy,
public ThreadingPolicy,
public CheckingPolicy {
// Policies are mixed in, no vtable
};
// Usage: Configure at compile time
using ThreadSafePtr = SmartPtr<HeapCreation, MultiThreaded, AssertCheck>;
using FastPtr = SmartPtr<HeapCreation, SingleThreaded, NoChecking>;
// Policies are just classes with required interface
struct HeapCreation {
template<class T>
static T* Create() { return new T; }
template<class T>
static void Destroy(T* p) { delete p; }
};
struct SingleThreaded {
struct Lock {
Lock() = default; // No-op
};
};
struct MultiThreaded {
struct Lock {
Lock() { /* acquire mutex */ }
~Lock() { /* release mutex */ }
};
};
// Type trait: Does T have a serialize() method?
template<typename T, typename = void>
struct has_serialize : std::false_type {};
template<typename T>
struct has_serialize<T,
std::void_t<decltype(std::declval<T>().serialize())>
> : std::true_type {};
// Use it for conditional behavior
template<typename T>
auto save(const T& obj) -> std::enable_if_t<has_serialize<T>::value> {
obj.serialize();
}
template<typename T>
auto save(const T& obj) -> std::enable_if_t<!has_serialize<T>::value> {
default_serialize(obj);
}
// C++20: Much cleaner with concepts
template<typename T>
concept Serializable = requires(T t) {
{ t.serialize() } -> std::convertible_to<std::string>;
};
void save(Serializable auto const& obj) {
obj.serialize();
}
// Type list: A compile-time list of types
template<typename... Ts>
struct TypeList {};
// Operations on type lists
template<typename List>
struct Length;
template<typename... Ts>
struct Length<TypeList<Ts...>> {
static constexpr size_t value = sizeof...(Ts);
};
// Get type at index
template<size_t I, typename List>
struct TypeAt;
template<typename Head, typename... Tail>
struct TypeAt<0, TypeList<Head, Tail...>> {
using type = Head;
};
template<size_t I, typename Head, typename... Tail>
struct TypeAt<I, TypeList<Head, Tail...>> {
using type = typename TypeAt<I - 1, TypeList<Tail...>>::type;
};
// Usage
using MyTypes = TypeList<int, double, std::string>;
static_assert(Length<MyTypes>::value == 3);
using Second = TypeAt<1, MyTypes>::type; // double
// Traditional visitor: Virtual dispatch at every node
// Alexandrescu approach: Static visitor with type list
template<typename... Types>
class Variant;
template<typename Visitor, typename Variant>
auto visit(Visitor&& v, Variant&& var) {
return var.visit(std::forward<Visitor>(v));
}
// Modern C++ (std::variant does this)
using Value = std::variant<int, double, std::string>;
auto result = std::visit(overloaded{
[](int i) { return std::to_string(i); },
[](double d) { return std::to_string(d); },
[](const std::string& s) { return s; }
}, value);
// The 'overloaded' helper (Alexandrescu-style)
template<class... Ts>
struct overloaded : Ts... {
using Ts::operator()...;
};
template<class... Ts>
overloaded(Ts...) -> overloaded<Ts...>;
Alexandrescu thinks of C++ templates as a compile-time functional language:
Alexandrescu later co-designed D, which incorporates many C++ template lessons:
These ideas now appear in modern C++ (constexpr, if constexpr, concepts).
Use Alexandrescu's techniques when:
Avoid when:
xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience