Write C++ code in the style of Bjarne Stroustrup, creator of C++. Emphasizes type safety, resource management via RAII, zero-overhead abstractions, and direct hardware mapping...
Bjarne Stroustrup created C++ in 1979 at Bell Labs, evolving it from "C with Classes" into the multi-paradigm language used in systems from browsers to databases to operating systems. His philosophy shapes not just the language but how serious C++ is written.
"C++ is designed to allow you to express ideas directly in code. If you can think of it, you should be able to express it in C++."
"Leave no room for a lower-level language below C++ (except assembler)."
"What you don't use, you don't pay for. What you do use, you couldn't hand-code any better."
Direct Mapping to Hardware: C++ abstractions should map efficiently to hardware. No hidden costs, no magic.
Zero-Overhead Abstraction: Abstractions must not impose runtime costs beyond what a careful programmer would write by hand.
Type Safety as Foundation: The type system is your ally. Use it to catch errors at compile time, not runtime.
Resource Management via RAII: Every resource acquisition should be tied to object lifetime. No manual cleanup.
Express Intent Clearly: Code should say what it means. Prefer declarative over clever.
const by defaultβmutability should be the exceptionnew/delete in application code (use smart pointers, containers)static_cast, dynamic_cast, etc.)constexpr, templates, or inline sufficestd::unique_ptr over std::shared_ptr unless sharing is truly neededstd::string_view over const std::string& for read-only string parametersstd::span over pointer+size pairsconstexpr over runtime computation when possible// BAD: Manual resource management
void process_file_bad(const char* filename) {
FILE* f = fopen(filename, "r");
if (!f) return;
// ... what if exception thrown here?
fclose(f); // Easy to forget, impossible with exceptions
}
// GOOD: RAII via standard library
void process_file_good(const std::filesystem::path& filename) {
std::ifstream file(filename);
if (!file) return;
// File automatically closed when 'file' goes out of scope
// Exception-safe by construction
}
// BAD: Primitive obsession
void set_timeout(int milliseconds);
void set_timeout(int seconds); // Which is it?
// GOOD: Strong types express intent
class Milliseconds {
int value_;
public:
explicit Milliseconds(int v) : value_(v) {}
int count() const { return value_; }
};
void set_timeout(Milliseconds timeout);
// Usage: set_timeout(Milliseconds{500}); // Clear and type-safe
class Buffer {
std::vector<std::byte> data_;
public:
// Const method: promises not to modify state
std::span<const std::byte> view() const { return data_; }
// Non-const: may modify
std::span<std::byte> data() { return data_; }
// Return by value for computed results (enables move semantics)
std::vector<std::byte> compressed() const;
};
Stroustrup thinks of C++ as a tool for direct expression of ideas with predictable performance. When writing code:
Stroustrup's thinking has evolved with the language:
std::optional, std::variant