Files
2025-10-25 03:02:53 +03:00

117 lines
5.1 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[expr.sizeof]
# 7 Expressions [[expr]](./#expr)
## 7.6 Compound expressions [[expr.compound]](expr.compound#expr.sizeof)
### 7.6.2 Unary expressions [[expr.unary]](expr.unary#expr.sizeof)
#### 7.6.2.5 Sizeof [expr.sizeof]
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L5607)
The sizeof operator yields the number of bytes
occupied by a non-potentially-overlapping object of the type
of its operand[.](#1.sentence-1)
The operand is either an expression,
which is an [unevaluated operand](expr.context#def:unevaluated_operand "7.2.3Context dependence[expr.context]"), or a parenthesized[*type-id*](dcl.name#nt:type-id "9.3.2Type names[dcl.name]")[.](#1.sentence-2)
The sizeof operator shall not be applied to an expression that
has function or incomplete type,
to the parenthesized name of such
types, or to a glvalue that designates a bit-field[.](#1.sentence-3)
The result of sizeof applied to any of the narrow character types is 1[.](#1.sentence-4)
The result ofsizeof applied to any other fundamental
type ([[basic.fundamental]](basic.fundamental "6.9.2Fundamental types")) is implementation-defined[.](#1.sentence-5)
[*Note [1](#note-1)*:
In particular, the values of sizeof(bool), sizeof(char16_t),sizeof(char32_t), and sizeof(wchar_t) are
implementation-defined[.](#1.sentence-6)[57](#footnote-57 "sizeof(bool) is not required to be 1.")
— *end note*]
[*Note [2](#note-2)*:
See [[intro.memory]](intro.memory "6.8.1Memory model") for the definition of byte
and [[basic.types.general]](basic.types.general#term.object.representation "6.9.1General") for the definition of object representation[.](#1.sentence-7)
— *end note*]
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L5641)
When applied to a reference type, the result is the size
of the referenced type[.](#2.sentence-1)
When applied to a class, the result is the number of bytes in an object
of that class including any padding required for placing objects of that
type in an array[.](#2.sentence-2)
The result of applying sizeof to a
potentially-overlapping subobject is
the size of the type, not the size of the subobject[.](#2.sentence-3)[58](#footnote-58 "The actual size of a potentially-overlapping subobject can be less than the result of applying sizeof to the subobject, due to virtual base classes and less strict padding requirements on potentially-overlapping subobjects.")
When applied to an array, the result is the total number of bytes in the
array[.](#2.sentence-4)
This implies that the size of an array of n elements isn times the size of an element[.](#2.sentence-5)
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L5663)
The lvalue-to-rvalue ([[conv.lval]](conv.lval "7.3.2Lvalue-to-rvalue conversion")),
array-to-pointer ([[conv.array]](conv.array "7.3.3Array-to-pointer conversion")), and
function-to-pointer ([[conv.func]](conv.func "7.3.4Function-to-pointer conversion")) standard conversions are not
applied to the operand of sizeof[.](#3.sentence-1)
If the operand is a prvalue,
the [temporary materialization conversion](conv.rval "7.3.5Temporary materialization conversion[conv.rval]") is applied[.](#3.sentence-2)
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L5672)
The [*identifier*](lex.name#nt:identifier "5.11Identifiers[lex.name]") in a sizeof... expression shall name a
pack[.](#4.sentence-1)
The sizeof... operator yields the number of elements
in the pack ([[temp.variadic]](temp.variadic "13.7.4Variadic templates"))[.](#4.sentence-2)
A sizeof... expression is a pack expansion ([[temp.variadic]](temp.variadic "13.7.4Variadic templates"))[.](#4.sentence-3)
[*Example [1](#example-1)*: template<class... Types>struct count {static constexpr std::size_t value = sizeof...(Types);}; — *end example*]
[5](#5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L5686)
The result of sizeof and sizeof... is a prvalue of typestd::size_t[.](#5.sentence-1)
[*Note [3](#note-3)*:
A sizeof expression
is an integral constant expression ([[expr.const]](expr.const "7.7Constant expressions"))[.](#5.sentence-2)
The [*typedef-name*](dcl.typedef#nt:typedef-name "9.2.4The typedef specifier[dcl.typedef]") std::size_t is declared in the standard header[<cstddef>](cstddef.syn#header:%3ccstddef%3e "17.2.1Header <cstddef> synopsis[cstddef.syn]") ([[cstddef.syn]](cstddef.syn "17.2.1Header <cstddef> synopsis"), [[support.types.layout]](support.types.layout "17.2.4Sizes, alignments, and offsets"))[.](#5.sentence-3)
— *end note*]
[57)](#footnote-57)[57)](#footnoteref-57)
sizeof(bool) is not required to be 1[.](#footnote-57.sentence-1)
[58)](#footnote-58)[58)](#footnoteref-58)
The actual size of a potentially-overlapping subobject
can be less than the result of
applying sizeof to the subobject, due to virtual base classes
and less strict padding requirements on potentially-overlapping subobjects[.](#footnote-58.sentence-1)