TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
sizeof operator is a compile-time unary operator that determines the size, in bytes, of a specific data type or an expression’s resulting type. It returns an unsigned integer of type std::size_t (defined in <cstddef>) representing the exact memory footprint required to store an object of that type, including any internal or trailing padding added by the compiler for alignment.
Syntax
The operator accepts three forms of operands:sizeof... variant strictly requires parentheses and applies only to variadic templates.
Core Mechanics
Unevaluated Context When an expression is passed tosizeof, it is treated as an unevaluated operand. The compiler resolves the type of the expression purely at compile-time. No runtime execution occurs, meaning any side effects within the expression are ignored.
char, signed char, and unsigned char is exactly 1. Therefore, sizeof measures memory in multiples of sizeof(char). A C++ byte consists of CHAR_BIT bits (defined in <climits>), which is typically, but not strictly required to be, 8 bits.
Arrays
When applied to a statically sized array, sizeof yields the total number of bytes occupied by the entire array, not the size of a pointer to its first element.
sizeof yields the size of the referenced type. It does not return the size of the underlying pointer implementing the reference.
class, struct, or union, the result includes the sizes of all member subobjects, base class subobjects, and any padding bytes inserted to satisfy the alignment requirements (alignof) of the architecture.
class or struct is guaranteed to have a size of at least 1 byte. This rule ensures that distinct objects of the same type have unique memory addresses.
vptr). The sizeof operator accounts for this overhead, resulting in a larger size than the sum of the explicitly declared member variables.
sizeof...)
Modern C++ provides the sizeof... operator specifically for variadic templates. Instead of returning a size in bytes, sizeof... queries the number of elements contained within a template parameter pack.
Restrictions
Thesizeof operator cannot be applied to the following:
- Incomplete types: Types lacking a full definition, such as forward-declared classes or arrays of unknown bound (e.g.,
extern int arr[];). - Function types: Though it is perfectly valid to apply it to pointers to functions.
- Bit-fields: You cannot query the size of a bit-field member directly.
- The
voidtype:voidis an incomplete type that cannot be completed.
Master C++ with Deep Grasping Methodology!Learn More





