C++
编译器支持
自由(freestanding)与宿主(hosted)
语言
标准库
标准库头文件
具名要求
特性测试宏 (C++20)
语言支持库
概念库 (C++20)
诊断库
内存管理库
元编程库 (C++11)
通用工具库
容器库
迭代器库
范围库 (C++20)
算法库
字符串库
文本处理库
数值库
日期和时间库
输入/输出库
文件系统库 (C++17)
并发支持库 (C++11)
执行控制库 (C++26)
技术规范
符号索引
外部库
[编辑] C++ 语言
通用主题
预处理器
注释
关键词
转义序列
流程控制
条件执行语句
if
switch
迭代语句(循环)
for
range-for (C++11)
while
do-while
跳转语句
continue - break
goto - return
函数
函数声明
Lambda 函数表达式
inline 说明符
动态异常规范 (直到 C++17*)
noexcept 说明符 (C++11)
异常
throw 表达式
try 块
catch 处理程序
命名空间
命名空间声明
命名空间别名
类型
基本类型
枚举类型
函数类型
类/结构体类型
联合类型
说明符
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
存储期说明符
初始化
默认初始化
值初始化
零初始化
复制初始化
直接初始化
聚合初始化
列表初始化 (C++11)
常量初始化
引用初始化
表达式
值类别
求值顺序
运算符
运算符优先级
替代表示
字面量
布尔 - 整数 - 浮点
字符 - 字符串 - nullptr (C++11)
用户定义 (C++11)
工具
属性 (C++11)
类型
typedef 声明
类型别名声明 (C++11)
类型转换
隐式转换
static_cast
const_cast
显式转换
dynamic_cast
reinterpret_cast
内存分配
new 表达式
delete 表达式
类
类声明
构造函数
this 指针
访问说明符
friend 说明符
类特有的函数属性
虚函数
override 说明符 (C++11)
final 说明符 (C++11)
explicit (C++11)
static
特殊成员函数
默认构造函数
复制构造函数
移动构造函数 (C++11)
复制赋值
移动赋值 (C++11)
析构函数
模板
类模板
函数模板
模板特化
参数包 (C++11)
杂项
内联汇编
C++ 历史
[编辑] 表达式
通用
值类别
求值顺序
常量表达式
主表达式
Lambda 表达式 (C++11)
Requires 表达式 (C++20)
包索引表达式 (C++26)
潜在求值表达式
字面量
整数字面量
浮点数字面量
布尔字面量
字符字面量
转义序列
字符串字面量
空指针字面量 (C++11)
用户定义字面量 (C++11)
运算符
赋值运算符
递增和递减
算术运算符
逻辑运算符
比较运算符
成员访问运算符
其他运算符
new 表达式
delete 表达式
throw 表达式
alignof
sizeof
sizeof... (C++11)
typeid
noexcept (C++11)
折叠表达式 (C++17)
运算符的替代表示
优先级和结合性
运算符重载
默认比较 (C++20)
转换
隐式转换
显式转换
常用算术转换
用户定义转换
const_cast
static_cast
dynamic_cast
reinterpret_cast
[编辑]
查询对象或类型的大小。
在需要知道对象实际大小时使用。
目录
1 语法
2 注意
3 关键词
4 示例
5 缺陷报告
6 参阅
[编辑] 语法
sizeof( 类型 )
(1)
sizeof 表达式
(2)
1) 返回类型的对象表示的字节大小。
2) 返回表达式类型的对象表示的字节大小,如果该表达式被求值。
类型
-
一个type-id(参见类型命名)
表达式
-
一个表达式,其运算符优先级不低于sizeof(例如,sizeof a + b 被解析为(sizeof a) + b,而不是sizeof (a + b))
sizeof表达式的结果是std::size_t类型的常量表达式。
[编辑] 注意
根据计算机架构,一个字节可能由8位或更多位组成,确切的位数记录在CHAR_BIT中。
以下sizeof表达式总是求值为1
sizeof(char)
sizeof(signed char)
sizeof(unsigned char)
sizeof(std::byte)
(C++17 起)
sizeof(char8_t)
(C++20 起)
sizeof不能用于函数类型、不完整类型或位域左值(C++11前)glvalue(C++11起)。
当应用于引用类型时,结果是引用类型的大小。
当应用于类类型时,结果是该类的完整对象所占用的字节数,包括将此类对象放置在数组中所需的任何额外填充。一个可能重叠的子对象所占用的字节数可能小于该对象的大小。
sizeof的结果总是非零,即使应用于空类类型。
当应用于表达式时,sizeof不评估表达式(即表达式是一个未评估的操作数)(C++11起),即使表达式指定一个多态对象,结果也是表达式的静态类型的大小。不执行左值到右值、数组到指针或函数到指针的转换。然而,对于prvalue参数会(形式上)执行临时物化:如果参数不可销毁,则程序格式错误。(C++17起)
[编辑] 关键词
sizeof
[编辑] 示例
示例输出对应于一个具有64位指针和32位int的系统(又称LP64 或 LLP64)。
运行此代码
#include
#include
struct Empty { };
struct Base { int a; };
struct Derived : Base { int b; };
struct Bit { unsigned bit: 1; };
struct CharChar { char c; char c2; };
struct CharCharInt { char c; char c2; int i; };
struct IntCharChar { int i; char c; char c2; };
struct CharIntChar { char c; int i; char c2; };
struct CharShortChar { char c; short s; char c2; };
int main()
{
Empty e;
Derived d;
Base& b = d;
[[maybe_unused]] Bit bit;
int a[10];
auto f = [&]() { return sizeof(int[10]) == sizeof a ? throw 1 : e; };
// f(); // the return type is Empty, but always throws 1
auto println = [](auto rem, std::size_t size) { std::cout << rem << size << '\n'; };
println( "1) sizeof empty class: ", sizeof e );
println( "2) sizeof pointer: ", sizeof &e );
println( "3) sizeof(Bit) class: ", sizeof(Bit) );
println( "4) sizeof(int[10]) array of 10 int: ", sizeof(int[10]) );
println( "5) sizeof a array of 10 int: ", sizeof a );
println( "6) length of array of 10 int: ", ((sizeof a) / (sizeof *a)) );
println( "7) length of array of 10 int (2): ", ((sizeof a) / (sizeof a[0])) );
println( "8) sizeof the Derived class: ", sizeof d );
println( "9) sizeof the Derived through Base: ", sizeof b );
println( "A) sizeof(unsigned): ", sizeof(unsigned) );
println( "B) sizeof(int): ", sizeof(int) );
println( "C) sizeof(short): ", sizeof(short) );
println( "D) sizeof(char): ", sizeof(char) );
println( "E) sizeof(CharChar): ", sizeof(CharChar) );
println( "F) sizeof(CharCharInt): ", sizeof(CharCharInt) );
println( "G) sizeof(IntCharChar): ", sizeof(IntCharChar) );
println( "H) sizeof(CharIntChar): ", sizeof(CharIntChar) );
println( "I) sizeof(CharShortChar): ", sizeof(CharShortChar) );
println( "J) sizeof f(): ", sizeof f() );
println( "K) sizeof Base::a: ", sizeof Base::a );
// println( "sizeof function: ", sizeof(void()) ); // error
// println( "sizeof incomplete type: ", sizeof(int[]) ); // error
// println( "sizeof bit-field: ", sizeof bit.bit ); // error
}
可能的输出
1) sizeof empty class: 1
2) sizeof pointer: 8
3) sizeof(Bit) class: 4
4) sizeof(int[10]) array of 10 int: 40
5) sizeof a array of 10 int: 40
6) length of array of 10 int: 10
7) length of array of 10 int (2): 10
8) sizeof the Derived class: 8
9) sizeof the Derived through Base: 4
A) sizeof(unsigned): 4
B) sizeof(int): 4
C) sizeof(short): 2
D) sizeof(char): 1
E) sizeof(CharChar): 2
F) sizeof(CharCharInt): 8
G) sizeof(IntCharChar): 8
H) sizeof(CharIntChar): 12
I) sizeof(CharShortChar): 6
J) sizeof f(): 1
K) sizeof Base::a: 4
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告
应用于
发布时的行为
正确的行为
CWG 1553
C++11
sizeof可用于位域xvalues
已禁止
[编辑] 另请参见
alignof (C++11)
查询类型的对齐要求(运算符)[编辑]
sizeof... 运算符 (C++11)
查询包中的元素数量[编辑]
numeric_limits
提供查询所有基本数值类型属性的接口 (类模板) [编辑]
C 文档 了解 sizeof