0 day streak
Code library

Code worth reading.

Hand-picked snippets across TypeScript, Python, Rust, Go, and more — each one comes with a line-by-line breakdown so you can read it like the people who wrote it.

3495 snippets · 10 languages
▶ Start learning
C++ · ExpertSnippet

SFINAE with std::enable_if

Substitution Failure Is Not An Error (SFINAE) allows the compiler to discard template overloads that would result in invalid code. std::enable_if_t leverages this to enable a function only when a specific compile-time condition (like being an integer) is met.

snippet.cpp
cpp
1
2
3
4
5
6
7
8
9
template <typename T>
std::enable_if_t<std::is_integral_v<T>, T>
increment(T value) {
return value + 1;
}
// Usage:
// auto a = increment(5); // Compiles
// auto b = increment(3.14); // Compiler Error
SyntaxFunctions
C++ · ExpertSnippet

The Curiously Recurring Template Pattern (CRTP)

CRTP is a technique where a class derives from a template instantiation using itself as an argument. It enables static polymorphism (compile-time dispatch), which avoids the overhead of virtual function tables (vtable).

snippet.cpp
cpp
1
2
3
4
5
6
7
8
9
10
11
12
template <typename Derived>
class Base {
public:
void interface() {
static_cast<Derived*>(this)->implementation();
}
};
class Derived : public Base<Derived> {
public:
void implementation() { /* ... */ }
};
OOPPerformance
C++ · ExpertSnippet

Placement New for Manual Memory Management

Placement new allows you to construct an object at a pre-allocated memory address. This is critical for high-performance systems and custom allocators where heap fragmentation or allocation overhead must be avoided.

snippet.cpp
cpp
1
2
3
4
5
char buffer[sizeof(MyClass)];
MyClass* obj = new (buffer) MyClass(42);
// Manual cleanup required:
obj->~MyClass();
Memory ManagementPerformance
C++ · ExpertSnippet

Perfect Forwarding with std::forward

Perfect forwarding ensures that a function template can pass its arguments to another function while preserving their original value category (lvalue or rvalue). This is achieved using universal references (T&&) and std::forward.

snippet.cpp
cpp
1
2
3
4
5
6
7
template <typename T>
void wrapper(T&& arg) {
target(std::forward<T>(arg));
}
// target(int&); // Lvalue
// target(int&&); // Rvalue
PerformanceFunctions
C++ · ExpertSnippet

Fold Expressions for Variadic Templates

Introduced in C++17, fold expressions simplify operations on variadic template parameter packs. Instead of recursive template calls, you can use a single expression to apply an operator across all elements in the pack.

snippet.cpp
cpp
1
2
3
4
5
6
7
template <typename... Args>
auto sum(Args... args) {
return (... + args);
}
// Usage:
// int total = sum(1, 2, 3, 4, 5); // 15
SyntaxFunctions
C++ · ExpertSnippet

C++20 Concepts and Requires Clauses

Concepts allow you to define semantic requirements on template arguments. Instead of cryptic compiler errors from deep within a template, the compiler checks the constraint at the call site, leading to much clearer error messages and better code documentation.

snippet.cpp
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <concepts>
#include <iostream>
template <typename T>
concept Arithmetic = std::is_arithmetic_v<T>;
template <Arithmetic T>
T calculate_mean(T a, T b) {
return (a + b) / 2;
}
int main() {
std::cout << calculate_mean(10, 20) << "\n";
// calculate_mean("string", "error"); // Compile error
}
SyntaxFunctions
C++ · ExpertSnippet

C++20 Coroutines: Minimal Promise Interface

C++20 coroutines are functions that can suspend and resume execution. They are controlled by a 'promise_type' which defines the behavior of the coroutine state machine, such as where it initially suspends.

snippet.cpp
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <coroutine>
#include <iostream>
struct Resumable {
struct promise_type {
Resumable get_return_object() { return {}; }
std::suspend_always initial_suspend() { return {}; }
std::suspend_always final_suspend() noexcept { return {}; }
void return_void() {}
void unhandled_exception() {}
};
};
Resumable simple_coro() {
std::cout << "Step 1\n";
co_await std::suspend_always{};
std::cout << "Step 2\n";
}
int main() {
auto handle = simple_coro(); // Suspended at start
}
FunctionsPerformance
C++ · ExpertSnippet

C++23 Explicit Object Parameters (Deduced This)

C++23 introduced 'deduced this', allowing member functions to take 'this' as an explicit parameter. This simplifies writing code that depends on the value category (lvalue/rvalue) or the cv-qualifiers of the calling object without duplicating functions.

snippet.cpp
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <typeinfo>
struct Logger {
template <typename Self>
void log_type(this Self&& self) {
std::cout << "Called on type: " << typeid(Self).name() << "\n";
}
};
int main() {
Logger l;
l.log_type(); // Deduces Logger&
std::move(l).log_type(); // Deduces Logger&&
}
SyntaxOOP