Skip to main content

112 docs tagged with "c++"

View all tags

Access Control

Access control (public, private, protected) enables encapsulation by controlling which code can access class members.

Aggregate Initialization

Initialize arrays and simple structs using brace-enclosed lists without constructors. Concise syntax for multiple members.

Alignment and offsetof

Alignment ensures data is placed at memory addresses divisible by its size, improving CPU access speed. Padding fills gaps to maintain alignment.

Allocators

Allocators are objects that manage memory allocation for STL containers. They provide a standardized interface for customizing how containers acquire and release memory.

Assembling Phase

The assembler converts human-readable assembly code into binary machine code (object files). Each assembly instruction becomes actual CPU instructions.

auto Type Deduction

auto lets the compiler deduce variable types from initializers, reducing verbosity and improving maintainability.

Build Systems and CMake

Build systems automate compilation, dependency management, and linking. They track changes and rebuild only what's necessary, making large C++ projects manageable.

C++ Basic Syntax

C++ syntax defines how code is written and structured. Understanding basic syntax is essential for writing valid C++ programs.

C++ Compilation Pipeline

The C++ compilation process transforms source code into executable binary through four main stages: preprocessing, compilation, assembly, and linking.

C++ Ecosystem & C vs C++

The C++ ecosystem encompasses compilers, build systems, package managers, IDEs, libraries, and testing frameworks. Understanding this landscape is crucial for effective C++ development.

Calling Conventions

Calling conventions define how functions receive parameters and return values at the assembly level - register usage, stack cleanup, and parameter passing order.

Chrono Library (Time)

The chrono library provides type-safe time utilities for durations, time points, and clocks. It's designed to prevent common time-related bugs through strong typing.

Class Memory Layout

Understanding how classes are laid out in memory is essential for optimization, debugging, and interfacing with other languages.

Class Templates

Class templates create generic classes that work with different types. Think std::unique_ptr - same class, different types.

Compilation Phase

The compilation phase translates preprocessed C++ code into assembly language. This is where syntax checking, semantic analysis, optimization, and code generation happen.

Concepts and Requires Expressions (C++20)

Concepts are named requirements for template arguments that replace SFINAE with readable constraints. Requires expressions are the building blocks that check if code compiles at compile-time.

const and volatile Qualifiers

CV-qualifiers (const and volatile) modify type behavior. const prevents modification; volatile prevents compiler optimization.

const Pointers

const with pointers creates three distinct scenarios. Understanding the difference prevents bugs and documents intent.

constexpr Functions

constexpr indicates values or functions can be evaluated at compile-time, enabling compile-time computation and optimization.

Constructors and Destructors

Constructors initialize objects and allocate resources. Destructors clean up when objects are destroyed. Together they enable RAII (Resource Acquisition Is Initialization).

Copy and Move Semantics

Copy creates a duplicate of an object. Move transfers ownership of resources from one object to another. Understanding when each happens is crucial for performance and correctness.

Cross-Compilation

Cross-compilation builds executables for a different platform (target) than the one running the compiler (host). Essential for embedded systems, mobile development, and deploying to different architectures.

Custom Deleters

Extend smart pointers to manage any resource requiring special cleanup beyond delete. Enable RAII for files, handles, connections, locks - anything needing cleanup.

decltype

decltype deduces the type of an expression, preserving references and const qualifiers exactly.

decltype(auto)

decltype(auto) (C++14) combines auto convenience with decltype precision - deduces type while preserving references and const.

Default Initialization

Object created without explicit initializer. Behavior depends on type and storage duration. Dangerous for fundamental types in local scope.

Differences Between C++ Standards

C++ evolves through standardized versions released approximately every three years. Each standard adds features, fixes issues, and modernizes the language while maintaining backward compatibility.

Dynamic vs Static Polymorphism

C++ supports two forms of polymorphism: dynamic (runtime, using virtual functions) and static (compile-time, using templates). Each has different trade-offs.

Expressions and Statements

Expressions produce values; statements perform actions. Understanding the distinction is fundamental to C++ programming.

Filesystem Library

The filesystem library (C++17) provides portable facilities for manipulating files and directories. It replaces platform-specific APIs and C-style file operations with a modern, type-safe interface.

Fold Expressions (C++17)

Fold expressions provide a concise way to apply operators to variadic template parameter packs. They eliminate the need for recursive template patterns.

Full Template Specialization

Full specialization provides a completely custom implementation for specific template arguments. It's like saying "for this exact type, use this completely different code."

Function Overloading

Function overloading allows multiple functions with the same name but different parameters. The compiler selects the best match based on arguments.

Function Templates

Function templates let you write one function that works with different types. The compiler generates specific versions for each type you use.

Fundamental Types

C++ provides built-in fundamental types for integers, floating-point numbers, characters, and booleans.

glvalue, prvalue, xvalue

C++11 refined value categories into five types: lvalue, prvalue, xvalue, glvalue, and rvalue. Understanding these enables perfect forwarding and move semantics.

Headers and Include Mechanism

Headers (.h, .hpp) contain declarations that are shared across multiple source files. The #include directive copies header contents into source files during preprocessing.

Inheritance

Inheritance lets you create new classes based on existing ones, reusing code and establishing "is-a" relationships. Derived classes inherit members from base classes and can add new functionality or override existing behavior.

Inline Functions

inline suggests the compiler replace function calls with function body, eliminating call overhead. Modern compilers decide automatically.

Input/Output Streams

The iostream library provides facilities for input/output operations through streams. It's a type-safe, extensible alternative to C's printf/scanf.

Iterators

Iterators are the glue between containers and algorithms. They provide a uniform interface for traversing and accessing elements in different container types, enabling generic algorithms.

Keywords and Tokens

Tokens are the smallest units of a C++ program. Keywords are reserved words with special meaning that cannot be used as identifiers.

Lambda Expressions

Lambdas (C++11) are anonymous functions that can capture variables from surrounding scope, enabling functional programming patterns and convenient callbacks.

Linking Process

The linker combines multiple object files and libraries into a single executable, resolving symbol references and assigning final memory addresses.

Lvalues and Rvalues

Every C++ expression has a type and a value category. Lvalues have persistent storage; rvalues are temporaries.

Makefiles

Makefiles define rules for building projects using the Make build system. They specify dependencies and commands to compile source code incrementally.

Memory Alignment

Data arranged at addresses that are multiples of its size. Required for correctness on some architectures, critical for performance on all.

Memory Model and Allocation

Understanding how C++ programs use memory is fundamental to writing efficient, safe code. Memory is divided into distinct regions with different characteristics and management strategies.

Multiple Inheritance

Multiple inheritance allows a class to inherit from multiple base classes. This enables combining functionality but introduces complexity, especially the diamond problem.

Name Mangling in C++

Name mangling (name decoration) encodes C++ function signatures into unique symbol names for the linker. This enables function overloading and namespaces while maintaining linkage compatibility.

new and delete Operators

Manual dynamic memory management in C++. Allocates on heap, requires explicit deallocation. Modern C++ prefers smart pointers.

noexcept Specifier

noexcept specifies that a function won't throw exceptions, enabling optimizations and stronger guarantees.

Object Files and Symbols

Object files (.o, .obj) are compiled but not yet linked binary files containing machine code, data, and metadata for the linker.

Object Lifetime

Object lifetime spans from construction to destruction. Understanding lifetime is critical for memory safety, RAII, and avoiding undefined behavior.

Object Slicing

Object slicing occurs when you copy a derived class object to a base class object. The derived parts are "sliced off" and lost.

Overview of CMake

A comprehensive, user-friendly guide to CMake organized for easy reference and learning.

Partial Template Specialization

Partial specialization lets you specialize templates for a pattern of types, not just one specific type. It's like "for all pointer types" or "for all pairs of same types."

Placement new

Constructs objects in pre-allocated memory without allocating. Separates construction from allocation for custom memory management.

Pointer Arithmetic

Pointer arithmetic navigates contiguous memory with automatic scaling by type size. Essential for arrays but dangerous without bounds checking.

Preprocessing in C++

The preprocessor is a text manipulation tool that runs before compilation. It handles #include, #define, #ifdef, and other directives, producing pure C++ code for the compiler.

Program Structure

C++ programs consist of one or more source files containing declarations, definitions, and the required main() function.

RAII (Resource Acquisition Is Initialization)

RAII is a fundamental C++ idiom where resource lifetime is tied to object lifetime. Resources are acquired in constructors and released in destructors, ensuring automatic cleanup and exception safety.

Ranges Library

Ranges (C++20) is a modern library that provides composable, lazy-evaluated operations on sequences. It replaces traditional iterator pairs with range objects and introduces views for efficient data transformation pipelines.

Raw Pointers

A pointer is a variable that stores a memory address, allowing indirect access to other variables.

Reference Collapsing Rules

When template type deduction creates "reference to reference", C++ applies collapsing rules. This enables perfect forwarding.

References

A reference is an alias - another name for an existing object. Unlike pointers, references cannot be null, must be initialized, and cannot be reseated.

Rule of 0/3/5

These rules tell you which special member functions to define based on your class's resource management needs.

SFINAE and enable_if

SFINAE (Substitution Failure Is Not An Error) is a fundamental C++ template mechanism that enables conditional template compilation. std::enable_if is the primary tool for applying SFINAE in practice.

Signed and Unsigned Types

Integer types can be signed (negative and positive) or unsigned (only positive). Understanding signedness prevents bugs and overflow issues.

Static vs Dynamic Linking

Linking can be static (library code copied into executable) or dynamic (library loaded at runtime). Each has trade-offs in size, deployment, and performance.

std::shared_ptr

Smart pointer with shared ownership via reference counting. Multiple shared_ptrs can own the same object, deleted when last owner destroyed.

std::unique_ptr

Smart pointer with exclusive ownership. Zero overhead, automatic cleanup, move-only semantics. The default choice for dynamic memory.

std::weak_ptr

Non-owning observer of shared_ptr-managed objects. Doesn't increase reference count, enables checking if object still exists.

STL Algorithms

STL algorithms are generic functions that work with any container through iterators. They provide tested, optimized implementations of common operations. Never write your own sort or search - use the STL!

STL Containers

Containers store collections of objects. The STL provides optimized, well-tested containers for different access patterns and performance needs. Choose the right container for your use case.

Storage Duration

Storage duration defines when and where objects are created and destroyed. C++ has four storage durations: automatic, static, dynamic, and thread.

Strict Aliasing Rule

Pointers of different types cannot point to the same memory (with exceptions). Enables compiler optimizations but causes undefined behavior when violated.

String Handling

C++ provides std::string_view (C++17) for non-owning string references. Rich API for searching, modifying, and converting text data.

Template Argument Deduction

Template argument deduction lets the compiler figure out template parameters from function arguments automatically. No need to write func(5) when func(5) works!

Translation Units

A translation unit is a single source file plus all its included headers after preprocessing. It's the basic unit of compilation in C++.

Type Traits

Type traits are compile-time tools that query and transform types. They power template metaprogramming and SFINAE, letting you write code that adapts to different types automatically.

Uniform Initialization

C++11 brace initialization {} - one syntax for all types. Consistent, safe (prevents narrowing), solves gotchas.

Value Initialization

Explicitly request zero-initialization for fundamentals, default construction for classes. Safer than default initialization.

Variadic Templates

Variadic templates accept any number of arguments of any types. They're the foundation for functions like std::make_tuple, printf, and perfect forwarding.

Virtual Functions and vtables

Virtual functions enable runtime polymorphism through dynamic dispatch. Understanding how they work (vtables) helps you understand their cost and use them effectively.

What is C++?

C++ is a general-purpose, compiled programming language that extends C with object-oriented,