0 day streak
Code library

Expert C++ exercises

Read these Expert C++ snippets line by line — each one comes with a written breakdown of what the code does and why.

C++ · Expert

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 s…

SyntaxFunctions
Open snippet →
C++ · Expert

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 overhea…

Memory ManagementPerformance
Open snippet →
C++ · Expert

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 re…

PerformanceFunctions
Open snippet →
C++ · Expert

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…

SyntaxFunctions
Open snippet →
C++ · Expert

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, leadin…

SyntaxFunctions
Open snippet →
C++ · Expert

Compile-time Enforcement with consteval

The 'consteval' keyword (C++20) specifies that a function must produce a compile-time constant. Unlike 'constexpr', which allows both compile-time and run-time execution, 'consteval' strictly forbi…

PerformanceBest Practices
Open snippet →
C++ · Expert

Cache-Optimized Row-Major Array Indexing

Modern CPUs use caches to speed up memory access. By flattening a 2D array into a 1D vector and using Row-Major indexing (incrementing columns in the inner loop), we ensure contiguous memory access…

Arrays & ListsPerformanceMemory Management
Open snippet →
C++ · Expert

Semantic Template Constraints using Concepts

C++20 Concepts provide a way to constrain template parameters with clear requirements. Unlike SFINAE, Concepts produce readable error messages and allow the compiler to reason about types semantica…

FunctionsSyntaxBest Practices
Open snippet →
C++ · Expert

Template-Based Loop Unrolling

This technique uses variadic templates and fold expressions to expand a loop into a sequence of function calls at compile-time. This eliminates branch overhead in performance-critical code.

Control FlowPerformanceFunctions
Open snippet →
C++ · Expert

Strided Array Access for Buffer Navigation

A manual implementation of strided access over a flat array. This is essential for processing multi-dimensional data (like image channels or matrix columns) stored in a contiguous memory block.

Arrays & ListsMemory ManagementSyntax
Open snippet →
C++ · Expert

Bitmask-Driven Control Logic

Using bitwise operators and enum classes to manage complex control flow states efficiently. This approach packs multiple boolean flags into a single byte, optimizing both memory and branch prediction.

Data TypesControl FlowPerformance
Open snippet →
C++ · Expert

In-place Construction in Raw Array Buffers

This example shows how to manage memory manually using placement new within a pre-allocated stack buffer. This technique avoids dynamic memory allocation overhead and ensures that objects are align…

Memory ManagementArrays & ListsPerformance
Open snippet →
C++ · Expert

Bit-level Metadata Embedding in Pointers

Since pointers on modern systems are often aligned to 4 or 8 bytes, the lowest bits are always zero. This snippet exploits this to store additional metadata (a 'tag') directly inside the pointer it…

Data TypesMemory ManagementPerformance
Open snippet →