首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

offsetof

Defined in header <cstddef>

?

?

#define offsetof(type, member) /*implementation-defined*/

?

?

offsetof扩展为类型的积分常量表达式。std::size_t,其值为从指定类型对象的开头到其指定成员的偏移量(以字节为单位),包括填充(如果有的话)。

如果type不是标准布局类型,该行为是未定义的。

如果member是静态构件或者是成员函数,该行为是未定义的。

标准布局类型的第一个成员的偏移量始终为零28。空基优化是强制%29。

例外

offsetof不抛出异常;表达式noexcept(offsetof(type, member))总是评估为true...

注记

offsetof必须按照上述规定工作,即使是一元。operator&对于所涉及的任何类型都是重载的。这不能在标准C++中实现,需要编译器的支持。

二次

代码语言:javascript
复制
#include <iostream>
#include <cstddef>
struct S {
    char c;
    double d;
};
int main()
{
    std::cout << "the first element is at offset " << offsetof(S, c) << '\n'
              << "the double is at offset " << offsetof(S, d) << '\n';
}

二次

可能的产出:

二次

代码语言:javascript
复制
the first element is at offset 0
the double is at offset 8

二次

另见

size_t

unsigned integer type returned by the sizeof operator (typedef)

is_standard_layout (C++11)

checks if a type is standard-layout type (class template)

c抵消文件

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com