Skip to main content

29 docs tagged with "cpp"

View all tags

Assertions and static_assert

Assertions are runtime or compile-time checks that verify program invariants and assumptions. They help catch bugs early during development and document expected conditions in code.

Atomics and Memory Fences

Atomic operations execute as single, indivisible steps with no interference from other threads. Memory fences (barriers) establish ordering constraints between operations.

C Interoperability

C++ can call C functions and vice versa using extern "C" linkage. Essential for using C libraries, system APIs, and creating C-compatible interfaces.

C++ Memory Model

The C++ memory model defines how threads interact through memory, including visibility of writes, ordering of operations, and synchronization primitives.

Condition Variables

Condition variables allow threads to wait for specific conditions to become true, enabling efficient thread coordination beyond simple mutexes.

Contracts (C++20)

Contracts are a programming paradigm that formally specifies the obligations and guarantees of code. They consist of preconditions (what callers must ensure), postconditions (what functions guarantee), and invariants (what must always be true).

Copy-and-Swap Idiom

Copy-and-swap is an elegant technique for implementing assignment operators that provides strong exception safety and eliminates code duplication by leveraging the copy constructor and a non-throwing swap function.

CRTP (Curiously Recurring Template Pattern)

CRTP is a template pattern where a class Derived inherits from a template base class Base, passing itself as a template argument. This enables compile-time polymorphism without virtual functions.

Data Races and Race Conditions

A data race occurs when two or more threads access the same memory location concurrently, at least one access is a write, and there's no synchronization between them. Data races cause undefined behavior.

Default Function Arguments

Default arguments allow function parameters to have default values when not explicitly provided by the caller.

Error Codes and std::error_code

Error codes provide an alternative to exceptions for error handling. They're explicit, predictable, and have zero overhead in the success case, making them ideal for performance-critical code and systems programming.

Exception Handling

Exceptions provide a mechanism to transfer control from a point where an error occurs to a handler that can deal with it. They separate error-handling code from normal logic, making code cleaner and more maintainable.

Futures and Promises

Futures and promises provide a mechanism for asynchronous computation: one thread computes a value and another retrieves it later.

Inline Assembly

Embed assembly instructions directly in C++ code for performance-critical operations, hardware access, or platform-specific features unavailable in C++.

Mutexes and Locks

A mutex (mutual exclusion) is a synchronization primitive that protects shared data by allowing only one thread to access it at a time.

noexcept and Strong Guarantee

The noexcept specifier indicates that a function won't throw exceptions. The strong exception guarantee ensures that operations either succeed completely or have no effect. Together, they enable writing robust, exception-safe code.

Non-Copyable Idiom

The non-copyable idiom prevents objects from being copied, ensuring unique ownership of resources. Essential for RAII types managing exclusive resources like file handles, mutexes, or database connections.

Object Layout and Memory Structure

How C++ objects are arranged in memory: data members, padding, vtables, base class subobjects. Understanding layout is crucial for binary compatibility and optimization.

Padding and offsetof

Padding bytes align struct members to hardware requirements. offsetof macro queries member positions. Understanding both optimizes memory usage and enables low-level memory manipulation.

Pimpl (Pointer to Implementation)

Pimpl (Pointer to Implementation) separates a class's interface from its implementation by moving private members into a separate implementation class. This reduces compilation dependencies and provides better encapsulation.

Policy-Based Design

Policy-based design decomposes complex behaviors into independent policy classes combined through templates. Each policy defines one aspect of behavior, and policies are mixed together to create flexible, reusable components.

Thread Management

Threads allow programs to perform multiple operations concurrently. C++11 introduced std::thread for portable threading.

Thread Pools

A thread pool manages a fixed set of worker threads that execute tasks from a queue, avoiding the overhead of creating and destroying threads repeatedly.

Type Erasure

Type erasure hides the concrete type behind a common interface, allowing different types to be treated uniformly without inheritance. Combines the flexibility of runtime polymorphism with the performance benefits of templates.

Undefined Behavior

Undefined Behavior (UB) occurs when the C++ standard places no requirements on what happens in a given situation. The program may crash, produce incorrect results, or appear to work correctly—there are no guarantees.

volatile Keyword

volatile tells compiler that a variable can change unexpectedly (hardware, interrupts, other threads). Prevents certain optimizations. Not for thread synchronization - use atomics instead.