Before writing real C++ programs, it is essential to understand the rules that govern how names are found, how objects are laid out in memory, and what kinds of expressions can appear on each side of an assignment. This page covers the core concepts that underpin every C++ program: how scope determines where names are visible, how linkage connects definitions across translation units, what the One Definition Rule requires, what the value categories lvalue and rvalue mean, and how alignment and trivial types affect object layout and performance.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/MicrosoftDocs/cpp-docs/llms.txt
Use this file to discover all available pages before exploring further.
Scope
Scope is the region of a program in which a name is visible. C++ defines several scope kinds:- Block scope — names declared inside
{}braces, visible from the point of declaration to the closing brace - Function scope — applies to labels (
gototargets) - Class scope — names declared inside a class definition
- Namespace scope — names declared in a namespace, including the global namespace
- File scope — names with external or internal linkage at the global level
:: lets you explicitly name the scope of an identifier:
Linkage
Linkage determines whether the same name in different translation units refers to the same entity.| Linkage | Meaning |
|---|---|
| External | Visible and usable across translation units (the default for non-const variables and functions at namespace scope) |
| Internal | Visible only within the translation unit where it is declared (static or const at namespace scope) |
| No linkage | Local variables, types declared inside a function |
Use
extern to declare (but not define) a variable that lives in another translation unit. Place the single definition in exactly one .cpp file and the extern declarations in a shared header.Translation Units and the ODR
A translation unit is a single.cpp source file after all #include directives have been processed. Each translation unit is compiled independently before being linked. The One Definition Rule (ODR) states:
- Every program must have exactly one definition of every non-inline function and non-inline variable with external linkage.
- Classes, inline functions, and templates may be defined in multiple translation units, but all definitions must be identical.
Header Files and Include Guards
Because class definitions and inline functions must appear in every translation unit that uses them, they are placed in header files. To prevent a header from being included more than once in the same translation unit, use an include guard or#pragma once:
Lvalues and Rvalues
Every C++ expression belongs to a value category that governs how it can be used. The C++17 standard defines:- lvalue — an expression that refers to a memory location and whose address can be taken (e.g., a named variable)
- prvalue (pure rvalue) — a temporary or literal with no persistent memory location
- xvalue (expiring value) — an object whose resources can be moved from (e.g., the result of
std::move) - glvalue = lvalue or xvalue
- rvalue = prvalue or xvalue
&&) bind to rvalues and enable move semantics:
Alignment
Alignment is a constraint on which memory addresses are valid for a given type. Misaligned access causes undefined behavior on many architectures and a hardware trap on some. Thealignof operator returns the required alignment of a type in bytes, and alignas overrides the default alignment:
Trivial, Standard-Layout, and POD Types
These categories determine how types interact with low-level features likememcpy, C APIs, and certain compiler optimizations.
Trivial Types
Trivial Types
A type is trivial if it has a compiler-generated (or
= default) default constructor, copy/move constructors, copy/move assignment operators, and destructor — with no virtual functions or virtual base classes. Trivial types can be safely memcpy-ed.Standard-Layout Types
Standard-Layout Types
A type is standard-layout if all members have the same access control, there are no virtual functions or virtual base classes, and all base classes and non-static data members form a compatible layout. Standard-layout types can be safely exchanged with C code.
POD Types (Plain Old Data)
POD Types (Plain Old Data)
A POD type is both trivial and standard-layout. POD types are the most compatible with C and are safe for use with
memcpy, binary serialization, and C-style APIs.Program Startup and Termination
A C++ program begins execution in themain function. Static and thread-local objects with non-trivial initialization are initialized before main begins; their destructors run after main returns or std::exit is called.
See Also
- Types and Declarations — built-in types,
auto,decltype, and type qualifiers - Classes and Structs — user-defined types, constructors, and inheritance
- Modern C++ — RAII, smart pointers, and move semantics